我正在使用 MKLocalSearch 搜索地点并将结果显示在 map 上。 问题是有时如果一家餐馆存在于另一个州,例如,它会去另一个州,我想将结果限制在我所在的地区。 这是我的代码
let searchRadius: CLLocationDistance = 10000
let region = MKCoordinateRegionMakeWithDistance(location.coordinate, searchRadius * 2.0, searchRadius * 2.0)
map.setRegion(region, animated: true)
let request = MKLocalSearchRequest()
request.naturalLanguageQuery = getRandomPlace()
request.region = map.region
let search = MKLocalSearch(request: request)
search.start { response, error in
guard let response = response else {
print("There was an error searching for: \(request.naturalLanguageQuery) error: \(error)")
return
}
for item in response.mapItems {
self.dropPinZoomIn(item.placemark)
}
}
最佳答案
您无法阻止响应包含跨越某些人为边界的结果。您已经为您的请求定义了一个区域
,结果将有利于该区域。但即便如此,docs很清楚:
Specifying a region does not guarantee that the results will all be inside the region. It is merely a hint to the search engine.
所以,你得到的就是你得到的。您无法阻止服务器返回它返回的结果。
如果您那么想要遍历返回的response
map 项目并且不为其中的一些项目制作地标,因为您不喜欢他们在哪里,这完全取决于你。但是您的代码不会那样做;你只是在盲目地为它们所有做地标。
关于ios - 将 MKLocalSearch 限制在一个区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42045168/