我正在尝试获取场地/企业名称的地址。对于场地名称,我还提供了城市和州,但成功率不到 10%。问题似乎在于找到位置而不是破译地址。
示例输入:“印第安纳州印第安纳波利斯爵士厨房”
典型输出:“找不到位置”
谢谢!
geocoder.geocodeAddressString(newSearch) { (placemarks, error) in
if let error = error {
print("Geocoding error: \(error.localizedDescription)")
geoError = true
}
if let placemark = placemarks?.first {
if let address = placemark.location?.coordinate {
print("Address: \(address)")
let webLoc = CLLocation(latitude: address.latitude, longitude: address.longitude)
uploadLoc(geoLoc: webLoc)
} else {
print("No address found")
geoError = true
}
} else {
print("No location found")
geoError = true
}
}
对于您问题中的示例请求,使用
MKLocalSearch
比 CLGeocoder
效果更好。
以下代码:
import MapKit
let req = MKLocalSearch.Request()
req.naturalLanguageQuery = "Jazz Kitchen Indianapolis,Indiana"
let srch = MKLocalSearch(request: req)
srch.start { resp, error in
if let resp {
print(resp.mapItems)
} else if let error {
print(error)
} else {
print("nothing")
}
}
给出以下结果:
[<MKMapItem: 0x600002df8d00> {
isCurrentLocation = 0;
name = "The Jazz Kitchen";
phoneNumber = "<redacted for SO>";
placemark = "The Jazz Kitchen, 5377 N College Ave, Indianapolis, IN 46220, United States @ <+39.85087740,-86.14554230> +/- 0.00m, region CLCircularRegion (identifier:'<+39.85087741,-86.14554230> radius 141.17', center:<+39.85087741,-86.14554230>, radius:141.17m)";
timeZone = "America/Indiana/Indianapolis (EST) offset -18000";
url = "http://www.thejazzkitchen.com";
}]