gpt4 book ai didi

swift - 为什么 MKLocalSearch for "nearby restaurants"返回的位置在不同的城市?

转载 作者:行者123 更新时间:2023-11-28 14:15:33 30 4
gpt4 key购买 nike

我正在构建一个简单的应用程序,允许用户保存他们的位置以备后用。

目标:用户在公司时输入他们的地址,我们保存公司名称

我的方法:我从 locationManager 请求位置。然后我将 CLLocation 反向地理编码为 CLPlacemark。由于地标无法识别商家名称,因此我开始使用 MKLocalSearch 搜索“附近的餐馆”。 response.mapItems 返回完全不同城市的位置。

我已指定区域并验证地标是否正确返回了用户地址。所以,我认为问题在于 MKLocalSearch。

为什么返回不同城市的结果?

已更新:来自 View Controller 的所有代码

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)
}
}

最佳答案

鉴于您的代码,您的逻辑似乎是正确的,但根据位置的不同,您可能无法获得想要的结果。 Apple 的 MKCoordinateRegion 文档指出“指定区域并不能保证结果都在该区域内。这只是对搜索引擎的提示。” MKLocalSearch Documentation

关于swift - 为什么 MKLocalSearch for "nearby restaurants"返回的位置在不同的城市?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52209447/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com