Skip to content

Instantly share code, notes, and snippets.

@esromneb
Created August 20, 2012 09:46
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save esromneb/3402711 to your computer and use it in GitHub Desktop.
Save esromneb/3402711 to your computer and use it in GitHub Desktop.
Convert MKMapRect to CLRegion aka convert visible region of mapview to CLRegion
- (CLLocationDistance)getDistanceFrom:(CLLocationCoordinate2D)start to:(CLLocationCoordinate2D)end
{
CLLocation *startLoc = [[CLLocation alloc] initWithLatitude:start.latitude longitude:start.longitude];
CLLocation *endLoc = [[CLLocation alloc] initWithLatitude:end.latitude longitude:end.longitude];
CLLocationDistance retVal = [startLoc distanceFromLocation:endLoc];
[startLoc release];
[endLoc release];
return retVal;
}
- (void) convert
{
MKMapRect mRect = self.mapview.visibleMapRect;
MKMapPoint neMapPoint = MKMapPointMake(MKMapRectGetMaxX(mRect), mRect.origin.y);
MKMapPoint swMapPoint = MKMapPointMake(mRect.origin.x, MKMapRectGetMaxY(mRect));
CLLocationCoordinate2D neCoord = MKCoordinateForMapPoint(neMapPoint);
CLLocationCoordinate2D swCoord = MKCoordinateForMapPoint(swMapPoint);
CLLocationDistance diameter = [self getDistanceFrom:neCoord to:swCoord];
CLRegion *region = [[CLRegion alloc] initCircularRegionWithCenter: mapview_.centerCoordinate radius:(diameter/2) identifier:@"mapWindow"];
//Now we can geocode an address inside the map's visible view...
//CLGeocoder *geocoder = [[CLGeocoder alloc] init];
//[geocoder geocodeAddressString:theSearchBar.text inRegion:region completionHandler:^(NSArray *placemarks, NSError *error) {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment