Created
August 20, 2012 09:46
-
-
Save esromneb/3402711 to your computer and use it in GitHub Desktop.
Convert MKMapRect to CLRegion aka convert visible region of mapview to CLRegion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (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