I'm trying to perform an MKLocalSearch, but only searching within a given radius

710 Views Asked by At

So let's say I'm trying to search for a query within 1 mile radius of the user's current location. Here's my search code so far:

- (void) performLocalSearch{
     MKLocalSearchRequest *searchRequest = [[MKLocalSearchRequest alloc] init];
     searchRequest.naturalLanguageQuery = _searchText;
     MKCoordinateRegion searchRegion;
     searchRegion.center.latitude = userLocation.coordinate.latitude;
     searchRegion.center.longitude = userLocation.coordinate.longitude;
     searchRegion.span.latitudeDelta = 0.0144927536231884;
     searchRegion.span.longitudeDelta = 0.0144927536231884;
     searchRequest.region = searchRegion;


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

     [search startWithCompletionHandler:^(MKLocalSearchResponse *response, NSError *error) {
          for (MKMapItem *item in response.mapItems) {
               MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
               annotation.title = item.name;
               annotation.coordinate = item.placemark.coordinate;

               [_map addAnnotation:annotation];
          }
      }
      ];
}

When I run the code on my device, pins show up on the map, but they are not within the specified radius. I also tried using MKCoordinateRegionMakeWithDistance to set the search region but I couldn't figure out how to cast the double radius value as a CLLocationDistance object. Any help would be appreciated. Thanks.

0

There are 0 best solutions below