CLGeocoder 对于纬度/经度总是 returns nil
CLGeocoder always returns nil for latitude / longitude
我正在尝试获取 latitude/longitude 坐标以用于基于位置的提醒 (Eventkit)
地址是有效的并且被正确地发送到函数,如果我为函数返回的坐标提供一个可选的,那么函数似乎可以工作,但地理编码器没有按预期转换地址。
我不明白为什么它不会。
var lat: Double?
var lon: Double?
func getLocation(address: String) -> CLLocation {
let geocoder = CLGeocoder()
geocoder.geocodeAddressString(address) { placemarks, error in
guard let placemark = placemarks?.first else { return }
let coordinate = placemark.location?.coordinate
lat = coordinate?.latitude //Is always nil
lon = coordinate?.longitude //Is always nil
}
// Note: providing ?? sample coordinates in the return below works
return CLLocation(latitude: lat!, longitude: lon!)
}
if self.locationChoice == "Dog's Home" {
let address = "\(self.dog.addressLine1),\(self.dog.town),\(self.dog.postcode)"
let structuredLocation = EKStructuredLocation(title: address)
structuredLocation.geoLocation = getLocation(address: address)// asking for CLLocation
structuredLocation.radius = 500
let alarm = EKAlarm()
if self.whenArrivingOrLeaving == 0 {
alarm.proximity = EKAlarmProximity.enter
}else {
alarm.proximity = EKAlarmProximity.leave
}
alarm.structuredLocation = structuredLocation
newReminder.addAlarm(alarm)
}
你不是零return你应该使用完成的异步方法的值
func getLocation(address: String,completion:@escaping(CLLocation? -> ())) {
let geocoder = CLGeocoder()
geocoder.geocodeAddressString(address) { placemarks, error in
guard let placemark = placemarks?.first else { return }
completion(placemark.location)
}
}
通话
getLocation(address: address) { loc in
// embed all code here
}
我正在尝试获取 latitude/longitude 坐标以用于基于位置的提醒 (Eventkit) 地址是有效的并且被正确地发送到函数,如果我为函数返回的坐标提供一个可选的,那么函数似乎可以工作,但地理编码器没有按预期转换地址。 我不明白为什么它不会。
var lat: Double?
var lon: Double?
func getLocation(address: String) -> CLLocation {
let geocoder = CLGeocoder()
geocoder.geocodeAddressString(address) { placemarks, error in
guard let placemark = placemarks?.first else { return }
let coordinate = placemark.location?.coordinate
lat = coordinate?.latitude //Is always nil
lon = coordinate?.longitude //Is always nil
}
// Note: providing ?? sample coordinates in the return below works
return CLLocation(latitude: lat!, longitude: lon!)
}
if self.locationChoice == "Dog's Home" {
let address = "\(self.dog.addressLine1),\(self.dog.town),\(self.dog.postcode)"
let structuredLocation = EKStructuredLocation(title: address)
structuredLocation.geoLocation = getLocation(address: address)// asking for CLLocation
structuredLocation.radius = 500
let alarm = EKAlarm()
if self.whenArrivingOrLeaving == 0 {
alarm.proximity = EKAlarmProximity.enter
}else {
alarm.proximity = EKAlarmProximity.leave
}
alarm.structuredLocation = structuredLocation
newReminder.addAlarm(alarm)
}
你不是零return你应该使用完成的异步方法的值
func getLocation(address: String,completion:@escaping(CLLocation? -> ())) {
let geocoder = CLGeocoder()
geocoder.geocodeAddressString(address) { placemarks, error in
guard let placemark = placemarks?.first else { return }
completion(placemark.location)
}
}
通话
getLocation(address: address) { loc in
// embed all code here
}