Jakub
Jakub

Reputation: 13860

CLLocation, geocoder and region search

I have problem with narrowing the Geocoder result. Here is my code:

CLLocationCoordinate2D centerLocation = CLLocationCoordinate2DMake(53, 10);
CLCircularRegion *region = [[CLCircularRegion alloc] initWithCenter: centerLocation radius:100 identifier:@"de"];
[self.geocoder geocodeAddressString:[NSString stringWithFormat:@"%@%@",textField.text,string] inRegion:region
                  completionHandler:^(NSArray* placemarks, NSError* error){
                         //filling the tableView code
                  }];

From apple docs:

"Specifying a region lets you prioritize the returned set of results to locations that are close to some specific geographical area"

But if i start tapping the nearest street name i get results from all over the world but not the street i want. I need to put entire street name in the field to get in in placemarks array. How can I get nearest-first result from geocoder?

EDIT:

I don't know if address is a street, postcode, city name or place name. The bahavior is similar to Map app where there is only one textfield to search in all places.

Upvotes: 1

Views: 978

Answers (1)

HiDeoo
HiDeoo

Reputation: 10563

"if i start tapping the nearest street name"

It looks like to me you’re doing a search based on user-entered query. You should use MKLocalSearch from Map Kit to perform searches for locations that a user can describe by name, address or type. Geocoding is used to convert map coordinates to a structured address or vice versa.

To quote the documentation:

Although local search and geocoding are similar, they support different use cases. Use geocoding when you want to convert between map coordinates and a structured address, such as an Address Book address. Use local search when you want to find a set of locations that match the user’s input.

For example:

MKLocalSearchRequest *request = [MKLocalSearchRequest new];
request.naturalLanguageQuery = @"saint";
request.region = MKCoordinateRegionMakeWithDistance(CLLocationCoordinate2DMake(48.850354, 2.337341), 500, 500);

MKLocalSearch *search = [[MKLocalSearch alloc] initWithRequest:request];

[search startWithCompletionHandler:^(MKLocalSearchResponse *response, NSError *error) {
    for (MKMapItem *item in response.mapItems) {
        NSLog(@"item.placemark.name = %@", item.placemark.name);
    }
}];

And I would get the following results which are correct based on the region i specified:

item.placemark.name = Eglise Saint Sulpice
item.placemark.name = J'Go Saint Germain
item.placemark.name = Terrazza Saint Germain
item.placemark.name = Hôtel Odéon Saint Germain
item.placemark.name = Le Bon Saint Pourcain
item.placemark.name = Hotel Le Relais Saint Germain
item.placemark.name = Piscine Saint Germain
item.placemark.name = Bistrot de la Grille Saint Germain
item.placemark.name = Esprit Saint Germain
item.placemark.name = Hôtel Relais Saint Sulpice

Then, you're free to do anything you want with the results you get.

Upvotes: 3

Related Questions