MKLocalSearch for nearby restaurants showing that of US and not current location

1.4k views Asked by At

currently using apple's map view for iOS app when requesting a mklocalsearch request of type "restaurant" (to display nearby restaurants) all i am getting is the restaurants of the US when running on a device instead of getting nearby restaurants in current Location (Lebanon).

this is the code of my view did load the result prints on the log restaurants of america

_mapview.showsUserLocation=YES;
_mapview.delegate=self;
MKLocalSearchRequest *request =
[[MKLocalSearchRequest alloc] init];
request.naturalLanguageQuery=@"Restaurant";
request.region = _mapview.region;

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

[search startWithCompletionHandler:^(MKLocalSearchResponse
                                     *response, NSError *error) {
    if (response.mapItems.count == 0)
        NSLog(@"No Matches");
    else
        for (MKMapItem *item in response.mapItems)
        {
            NSLog(@"name = %@", item.name);
            NSLog(@"Phone = %@", item.phoneNumber);
        }
}];
1

There are 1 answers

0
jamalkhattab On BEST ANSWER

figured it out! we must set the region of the map view manually or it will assume the region is in the US by default. this code is complete and will display nearby restaurants in your location

[super viewDidLoad];
[self.searchDisplayController setDelegate:self];
[self.ibSearchBar setDelegate:self];

self.ibMapView.delegate=self;

// Zoom the map to current location.
[self.ibMapView setShowsUserLocation:YES];
[self.ibMapView setUserInteractionEnabled:YES];
[self.ibMapView setUserTrackingMode:MKUserTrackingModeFollow];



CLLocationManager *locationManager = [[CLLocationManager alloc] init];
locationManager.delegate=self;

[locationManager startUpdatingLocation];
[self.ibMapView setRegion:MKCoordinateRegionMake(locationManager.location.coordinate, MKCoordinateSpanMake(0.2, 0.2))];

MKLocalSearchRequest *request = [[MKLocalSearchRequest alloc] init];
request.region = self.ibMapView.region;
request.naturalLanguageQuery = @"restaurant";

[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
localSearch = [[MKLocalSearch alloc] initWithRequest:request];

[localSearch startWithCompletionHandler:^(MKLocalSearchResponse *response, NSError *error){

    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

    results = response;
    if (response.mapItems.count == 0)
        NSLog(@"No Matches");
    else
        for (MKMapItem *item in response.mapItems)
        {
            NSLog(@"name = %@", item.name);
            NSLog(@"Phone = %@", item.phoneNumber);

            [_matchingItems addObject:item];
            MKPointAnnotation *annotation =
            [[MKPointAnnotation alloc]init];
            annotation.coordinate = item.placemark.coordinate;
            annotation.title = item.name;
            [self.ibMapView addAnnotation:annotation];
        }
}];

}