I need to fit certain borders inside the map. I get the bounds from calling google geocoder and reading the viewport property, which looks like this:
{
northeast = {
lat = "30.4212235";
lng = "-97.486942";
};
southwest = {
lat = "30.1128403";
lng = "-97.99917959999999";
};
}
Then I convert them to CLLocationCoordinate2D
NSDictionary *viewport = [[[results objectAtIndex:0] objectForKey:@"geometry"]
objectForKey:@"viewport"];
NSDictionary *NEDictionary = [viewport objectForKey:@"northeast"];
NSDictionary *SWDictionary = [viewport objectForKey:@"southwest"];
CLLocationCoordinate2D SWCoordinate =
CLLocationCoordinate2DMake(
[[SWDictionary objectForKey:@"lat"] floatValue],
[[SWDictionary objectForKey:@"lng"] floatValue]
);
CLLocationCoordinate2D NECoordinate =
CLLocationCoordinate2DMake(
[[NEDictionary objectForKey:@"lat"] floatValue],
[[NEDictionary objectForKey:@"lng"] floatValue]
);
I know that I need to create MKMapRect (or MKMapRegion, whichever is easier) from these coordinates, and then [mapView setVisibleRect: newRect animated: YES] (or [mapView setRegion: newRegion animated: YES], but I not quite sure how to get there, I need a method to transform the borders into the correct data structure, for example:
- (MKMapRect) mapRectThatFitsBoundsSW:(CLLocationCoordinate2D)sw
NE:(CLLocationCoordinate2D)ne {
MKMapRect mapRectFromBounds = MKMapRectMake(x,y,width,height);
return mapRectFromBounds;
}
Any thoughts?
source
share