public
Last active

Convert MKMapRect to CLRegion aka convert visible region of mapview to CLRegion

  • Download Gist
convert.m
Objective-C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
- (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) {}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.