如何在发布任何项目之前获取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()
}
更新:我决定使用Google Places SDK for iOS https://developers.google.com/places/ios-sdk/intro来实现此目的。它提供了比CLLocation更灵活,更可靠的解决方案。
答案 0 :(得分: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()
}
如果找不到地址,我使用了??
,然后就像现在一样使用 locality 。与完成块内的result
一起玩,检查其他可用选项,以防其中一个对您有吸引力。
问候。