Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get multiple placemarks from CLGeocoder

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?

like image 389
johsem Avatar asked May 02 '12 15:05

johsem


People also ask

How to Reverse geocode in swift?

Reverse Geocoding With Swift. Reverse geocoding is a simple concept. We hand the CLGeocoder class a set of coordinates, latitude and longitude, and ask it for the corresponding address, a physical location that has meaning to the user.

What is CLGeocoder?

An interface for converting between geographic coordinates and place names.

How get lat long from address in IOS Swift?

geocodeAddressString(addressString) { (placemarks, error) in if error == nil { if let placemark = placemarks?[0] { let location = placemark. location! completionHandler(location. coordinate, nil) return } } completionHandler(kCLLocationCoordinate2DInvalid, error as NSError?) } }


1 Answers

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  
 }];
like image 144
Richard H Fung Avatar answered Oct 04 '22 14:10

Richard H Fung