How to get multiple placemarks from CLGeocoder
Asked Answered
O

2

8

Whatever address i give to the geocoder ([geocoder geocodeAddressString:completionHandler:), it always puts only one object in the placemarks array.

I there any way to get multiple results (like in Maps app) from which the user can select one?

Oakie answered 2/5, 2012 at 15:36 Comment(0)
U
3

I've done some sniffing on the packets and it seems that CLGeocoder doesn't connect to Google's geocoding service, but to Apple's. I've also noticed that I get only one placemark from there every time.

If you want something more sophisticated you should use Google's or other geocoding. I use SVGeocoder (https://github.com/samvermette/SVGeocoder), which has a very similar API to CLGeocoder.

Uncover answered 2/6, 2012 at 17:34 Comment(4)
Why does this happen? From the Apple Docs: In the case of forward-geocoding requests, multiple placemark objects may be returned if the provided information yielded multiple possible locations.Twelve
I suppose Apple's geocoding service is pretty limited as of now. I would expect it to change soonish. You can see that Apple is just rushing to move away from dependence on Google here.Cacography
It's interesting as there still seem to be issues with multiple placemark returns from CLGeocoder although the iOS 6 native maps search solution seems to implement exactly what everyone is looking for. I wonder what solution Apple are using themselves at the momentAmbidextrous
SVGeocoder uses the Google geocoder service which you can not use without displaying the results on a google map by the way.Flophouse
M
11

Apple's native geocoding service is provided by the MapKit framework. The important object in this framework is MKLocalSearch, which can geocode addresses and return multiple results.

MKLocalSearch returns back 10 results in mapItems of type MKMapItem. Each MKMapItem contains a MKPlacemark object, which is a subclass of CLPlacemark.

Here's an example using MapKit's MKLocalSearch:

MKLocalSearchRequest* request = [[MKLocalSearchRequest alloc] init];
request.naturalLanguageQuery = @"Calgary Tower";
request.region = MKCoordinateRegionMakeWithDistance(loc, kSearchMapBoundingBoxDistanceInMetres, kSearchMapBoundingBoxDistanceInMetres);

MKLocalSearch* search = [[MKLocalSearch alloc] initWithRequest:request];
[search startWithCompletionHandler:^(MKLocalSearchResponse *response, NSError *error) {
   yourArray = response.mapItems; // array of MKMapItems
   // .. do you other logic here  
 }];
Magenmagena answered 22/4, 2013 at 21:16 Comment(0)
U
3

I've done some sniffing on the packets and it seems that CLGeocoder doesn't connect to Google's geocoding service, but to Apple's. I've also noticed that I get only one placemark from there every time.

If you want something more sophisticated you should use Google's or other geocoding. I use SVGeocoder (https://github.com/samvermette/SVGeocoder), which has a very similar API to CLGeocoder.

Uncover answered 2/6, 2012 at 17:34 Comment(4)
Why does this happen? From the Apple Docs: In the case of forward-geocoding requests, multiple placemark objects may be returned if the provided information yielded multiple possible locations.Twelve
I suppose Apple's geocoding service is pretty limited as of now. I would expect it to change soonish. You can see that Apple is just rushing to move away from dependence on Google here.Cacography
It's interesting as there still seem to be issues with multiple placemark returns from CLGeocoder although the iOS 6 native maps search solution seems to implement exactly what everyone is looking for. I wonder what solution Apple are using themselves at the momentAmbidextrous
SVGeocoder uses the Google geocoder service which you can not use without displaying the results on a google map by the way.Flophouse

© 2022 - 2024 — McMap. All rights reserved.