使用CoreLocation获取最近的位置

问题描述 投票:0回答:1

如何在发布任何项目之前获取Instagram当前位置的最近位置?我使用CoreLocation通过下面的代码查找我当前的位置。现在我如何使用此CLLocation信息类来获取最近的位置?或者我还有哪些其他选项可以实现此功能?任何建议都会非常有帮助。

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]){
    CLGeocoder().reverseGeocodeLocation(manager.location!, completionHandler: { (placemarks, error) -> Void in
        if (error != nil){
            print("LoginView locationManager Error: \(error!.localizedDescription)");
            return
        }

        if (placemarks!.count > 0){
            let placemark = placemarks?[0]

            let latitude = String(format: "%f", (placemark?.location?.coordinate.latitude)!)
            let longitude = String(format: "%f", (placemark?.location?.coordinate.longitude)!)
            guard let local = placemark?.locality else { return }
            let uLocation = UserLocation(locality: local, longitude: longitude, latitude: latitude)
            self.userLocations.append(uLocation)
            self.locationContainer.setupData(locations: self.userLocations, delegate: self)
            self.locationManager.stopUpdatingLocation()
        }
    })
    locationManager.stopUpdatingLocation()
}

更新:我决定为此目的使用适用于iOS https://developers.google.com/places/ios-sdk/intro的Google Places SDK。它提供了比CLLocation更灵活,更可靠的解决方案。

ios swift core-location geofencing
1个回答
0
投票
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    CLGeocoder().reverseGeocodeLocation(manager.location!, completionHandler: { (placemarks, error) -> Void in
        if (error != nil){
            print("LoginView locationManager Error: \(error!.localizedDescription)");
            return
        }

        if (placemarks!.count > 0){
            let placemark = placemarks?[0]

            let latitude = String(format: "%f", (placemark?.location?.coordinate.latitude)!)
            let longitude = String(format: "%f", (placemark?.location?.coordinate.longitude)!)
            self.addressDecoder.reverseGeocodeLocation(manager.location!, completionHandler: {
                [unowned self] (result, error) in
                guard let address = result else { return }
                let nearbyName = address[0].name ?? address[0].locality
                let uLocation = UserLocation(locality: local, longitude: longitude, latitude: latitude)
                self.userLocations.append(uLocation)
            })
            let uLocation = UserLocation(locality: local, longitude: longitude, latitude: latitude)
            self.userLocations.append(uLocation)
            self.locationContainer.setupData(locations: self.userLocations, delegate: self)
            self.locationManager.stopUpdatingLocation()
        } else {
        }
    })
    locationManager.stopUpdatingLocation()
}

我使用??以防万一找不到地址,然后就像你现在一样去当地。在完成区内玩result以检查其他选项,以防其中一个对您有吸引力。

问候。

© www.soinside.com 2019 - 2024. All rights reserved.