[英]Why is MKLocalSearch for “nearby restaurants” returning locations in different cities?
I'm building aa simple application that allows a user to save their location for later. 我正在构建一个简单的应用程序,允许用户保存其位置以供以后使用。
The Goal: A user pins their address when at a business, and we save the name of the business 目标:用户在公司时固定其地址,我们保存公司名称
My approach: I'm requesting the location from a locationManager. 我的方法:我正在从locationManager请求位置。 Then I reverse geocode the CLLocation into a CLPlacemark.
然后,我将CLLocation反向地理编码为CLPlacemark。 Since the placemark isn't recognizing the business name, I start a MKLocalSearch for "restaurants nearby".
由于地标无法识别商户名称,因此我对“附近的餐馆”启动了MKLocalSearch。 The response.mapItems are returning locations in completely different cities.
response.mapItems将返回完全不同的城市中的位置。
I have specified the region and verified that the placemark is correctly returning the user's address. 我已经指定了区域并验证了地标正确返回了用户的地址。 So, I believe the issue lays within the MKLocalSearch.
因此,我认为问题在于MKLocalSearch。
Why does it return results for different cities? 为什么返回不同城市的结果?
Updated: All code From View Controller 更新:来自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)
}
}
Given your code, it appears your logic is correct, but depending on the location, you may not get what you want. 给定您的代码,看来您的逻辑是正确的,但是根据位置,您可能无法获得所需的信息。 Apple's documentation for the
MKCoordinateRegion
states that "specifying a region does not guarantee that the results will all be inside the region. It is merely a hint to the search engine." 苹果公司针对
MKCoordinateRegion
的文档指出:“指定区域并不能保证结果将全部在区域内。这只是对搜索引擎的提示。” MKLocalSearch Documentation MKLocalSearch文档
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.