Issue
I am using MKLocalSearch to search for places and display the result on the map.
The problem is that sometimes if a restaurant exists in another state for example, it goes to the another state, i want to limit results to my region.
Here is my code
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)
}
}
Solution
You cannot prevent the response from including results that are across some artificial boundary. You have defined a region
for your request and the results will favor that region. But even then, the docs are quite clear:
Specifying a region does not guarantee that the results will all be inside the region. It is merely a hint to the search engine.
So, what you get is what you get. You cannot prevent the server from returning the results that it returns.
If you then want to run through the returned response
map items and not make placemarks for some of them because you don’t like where they are, that is entirely up to you. But your code does not do that; you are just blindly making placemarks for all of them.
Answered By – matt
Answer Checked By – Terry (BugsFixing Volunteer)