I would like to enable the users of my iOS app to search for nearby locations (mainly streets and POIs). The behavior should be pretty much the same like when searching for an address in the native Apple Maps App. I first tried to implement this by using MKLocalSearch but I only received POIs as results and wasn't able to search for nearby streets for example. I don't see any possibility to alter the search behavior of MKLocalSearch.
Here is my code:
let request = MKLocalSearchRequest()
request.region = mapRegion!
request.naturalLanguageQuery = searchText
let localSearch = MKLocalSearch(request: request)
localSearch.startWithCompletionHandler({(response: MKLocalSearchResponse?, error: NSError?) -> Void in
if error != nil {
print("Error during local search: \(error!)")
}
if response != nil {
self.placemarks = response!.mapItems
self.tableView.reloadData()
}
})
In my second attempt I tried using CLGeocoder but the results where so bad compared to the native app results. Most of the top results where located in the US event though I'm in europe. I double checked the value for my current location and it's definitely set to my location using a circular search radius of 1000 meter.
Here is the code:
let region = CLCircularRegion.init(center: currentLocation, radius: 1000, identifier: "")
geoCoder.geocodeAddressString(searchText, inRegion: region, completionHandler:{(response: [CLPlacemark]?, error: NSError?) -> Void in
if error != nil {
print("Error during local search: \(error!)")
}
if let result = response {
self.placemarks = result
self.tableView.reloadData()
}
})
Am I doing something wrong or are both APIs really that bad? My last attempt would be to implement this functionality by using the Google maps API. As I'm currently using the Apple maps (MKMapView) I would probably have to replace the map itself as well because Google doesn't allow showing their geocoder results on an Apple map, am I right?
Would be a huge disappointment if I'm not able to get this functionality working with MapKit in Europe but maybe it works just perfect in the US.