Ammaiappan
Ammaiappan

Reputation: 539

Apple CLGeocoder,MKLocalSearch result differ from device map result

In my application search with the location string, it ll return the 5 result, but that not match with default device map app.

my code 1 : CLGeocoder

CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:searchKey completionHandler:^(NSArray* placemarks, NSError* error){
    dispatch_async(dispatch_get_main_queue(), ^{
        if (placemarks.count > 0) {
            searchLocations = placemarks;
        } else {
            searchLocations = nil;
        }
        [searchTableView reloadData];

        for (CLPlacemark *placemark in placemarks) {
            NSLog(@"place dic %@",placemark.addressDictionary);
        }
    });
}];

Code 2 : MKLocalSearch

CLLocation *userLoc = (CLLocation *)[[MYLocationManager defaultManager] currentLocation];
MKLocalSearchRequest *request = [[MKLocalSearchRequest alloc] init];
request.region = MKCoordinateRegionMakeWithDistance(userLoc.coordinate, 100000, 100000);
request.naturalLanguageQuery = searchKey;
MKLocalSearch *search = [[MKLocalSearch alloc] initWithRequest:request];
[search startWithCompletionHandler:^(MKLocalSearchResponse * _Nullable response, NSError * _Nullable error) {
    NSMutableArray *locs = [[NSMutableArray alloc]init];
    for (MKMapItem *placeM in response.mapItems) {
        [locs addObject:placeM.placemark];
    }
    if (locs.count > 0) {
        searchLocations = locs;
    } else {
        searchLocations = nil;
    }


    dispatch_async(dispatch_get_main_queue(), ^{
    [searchTableView reloadData];
    });
}];

Both are return a same result CLGeocoder result

Device map app result :

DEvice map result

Device map result differ from coding geo results. please help to solve this. and my question is what type of search methodology use the default map application? and how to get same result in coding ?

Upvotes: 3

Views: 903

Answers (2)

Gerd Castan
Gerd Castan

Reputation: 6859

The result of the query depends on the region you're searching in.

That's the 'Local' in MKLocalSearch.

The location in your code example is the user's location in a region with a side length on 10km.

The region where the built in map searches should be equal to or dependant on the region you're showing in the App at this very moment.

The region in Apples App is probably very different from the explicit region in your App.

Upvotes: 0

Amol Prakash
Amol Prakash

Reputation: 257

Please refer this answer. It describe all the steps needed to replicate the above result. I've tested this on simulator.

Upvotes: 0

Related Questions