swift - 为什么 MKLocalSearch 搜索“附近的餐馆”会返回不同城市的位置?
问题描述
我正在构建一个简单的应用程序,允许用户保存他们的位置以供以后使用。
目标:用户在企业中使用他们的地址,我们保存企业名称
我的方法:我从 locationManager 请求位置。然后我将 CLLocation 反向地理编码为 CLPlacemark。由于地标无法识别公司名称,我为“附近的餐馆”启动了 MKLocalSearch。response.mapItems 返回完全不同城市的位置。
我已经指定了区域并验证了地标是否正确地返回了用户的地址。所以,我相信问题在于 MKLocalSearch。
为什么它返回不同城市的结果?
更新:来自视图控制器的所有代码
class ViewController: UIViewController {
let locationManager = CLLocationManager()
var places = [MKMapItem]()
let naturalLanguageQuery = "closest places to eat"
let queries = ["restaurants", "places to eat", "breakfast", "lunch", "dinner"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading here
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if CLLocationManager.authorizationStatus() == .notDetermined {
locationManager.requestWhenInUseAuthorization()
}
}
@IBAction func getLocation() {
if CLLocationManager.authorizationStatus() == .authorizedWhenInUse {
locationManager.requestLocation()
}
}
func add(placemark: CLPlacemark) {
search(placemark: placemark, index: self.queries.count - 1)
}
func searchCompleted(placemark: CLPlacemark) {
guard let foo = Scraper.shared.sortByDistance(userPlacemark: placemark, items: places) else { return }
for item in Scraper.shared.filterForUnique(items: foo) {
print(item)
if item.placemark.addressString == placemark.addressString {
}
}
}
func search(placemark: CLPlacemark, index: Int) {
let request = MKLocalSearchRequest()
guard let coordinate = placemark.location?.coordinate else { return }
request.region = MKCoordinateRegionMakeWithDistance(coordinate, 1600, 1600)
request.naturalLanguageQuery = queries[index]
MKLocalSearch(request: request).start { (response, error) in
guard error == nil else { return }
guard let response = response else { return }
guard response.mapItems.count > 0 else { return }
for item in response.mapItems {
self.places.append(item)
}
if index != 0 {
self.search(placemark: placemark, index: index - 1)
} else {
self.searchCompleted(placemark: placemark)
}
}
}
}
extension ViewController: CLLocationManagerDelegate {
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let geocoder = CLGeocoder()
if let loc = locations.last {
geocoder.reverseGeocodeLocation(loc) { (placemarks, error) in
if let error = error {
print("error")
} else {
if let placemark = placemarks?.first {
print(placemark.debugDescription)
self.add(placemark: placemark)
}
}
}
}
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print(error.localizedDescription)
}
}
解决方案
鉴于您的代码,您的逻辑似乎是正确的,但根据位置,您可能无法得到您想要的。苹果公司的文件MKCoordinateRegion
指出,“指定一个区域并不能保证结果都在该区域内。它只是对搜索引擎的一个提示。” MKLocalSearch 文档
推荐阅读
- vim - 除非我在 repl 中切换到命名空间,否则表单评估会运行整个文件
- unity3d - 有 Unity 类似手机的多人游戏系统吗?
- python-3.x - 如何在当前行之前 60 秒返回行的索引
- arm - ARMv6M ADD(register) T2 Encoding 规范中的“DN”是什么?
- c# - 从网站上抓取的字符串有 ' ' 输出为 '?'
- sql - 如何添加带整数的日期
- javascript - SyntaxError:位置 2 处 JSON 中的意外标记 v
- typo3 - tx_news 翻译中的错误内容元素
- javascript - Chrome 扩展程序(页面上的内容脚本)是否可以访问 TINYMCE 编辑器中的光标位置?
- omnet++ - 如何在 omnet++ 中记录结果