I'm familiar with the Google Maps API and I'm trying to learned the iOS MapKit library now. I have an iPhone application which takes an input string and geocodes it using Google Maps geocoder service. I also want to set an appropriate zoom level for the new map but I'm not quite sure how to do it.
After reading, Determining zoom level from single LatLong in Google Maps API
My plan was to parse the JSON response from the Google Maps API and extract the ExtendedData field.
"ExtendedData":{
"LatLonBox":{
"north":34.13919,
开发者_C百科 "south":34.067018,
"east":-118.38971,
"west":-118.442796
}
Then using that I would like to set the bounds of my map accordingly using the MapKit setRegion function.
I started laying out a function to do this, but I'm a little lost on the logic...
- (void) setMapZoomForLocation(CLLocationCoordinate2D location, double north, double south, double east, double west){
// some fancy math here....
// set map region
MKCoordinateRegion region;
region.center = location;
MKCoordinateSpan span;
// set the span so that the map bounds are correct
span.latitudeDelta = ???;
span.longitudeDelta = ???;
region.span = span;
[self.mapView setRegion:region animated:YES];
}
Alternatively, I guess I could just used the Accuracy result from a geocode result to set a sort of default zoom level. I'm just not sure how to compute the equivalent default zoom levels for the various results.
See https://code.google.com/apis/maps/documentation/geocoding/v2/#GeocodingAccuracy
------------------------ Update: Solution I Used ------------------------------
// parse json result
NSDictionary *results = [jsonString JSONValue];
NSArray *placemarks = (NSArray *) [results objectForKey:@"Placemark"];
NSDictionary *firstPlacemark = [placemarks objectAtIndex:0];
// parse out location
NSDictionary *point = (NSDictionary *) [firstPlacemark objectForKey:@"Point"];
NSArray *coordinates = (NSArray *) [point objectForKey:@"coordinates"];
CLLocationCoordinate2D location;
location.latitude = [[coordinates objectAtIndex:1] doubleValue];
location.longitude = [[coordinates objectAtIndex:0] doubleValue];
// DEBUG
//NSLog(@"Parsed Location: (%g,%g)", location.latitude, location.longitude);
// parse out suggested bounding box
NSDictionary *extendedData = (NSDictionary *) [firstPlacemark objectForKey:@"ExtendedData"];
NSDictionary *latLngBox = (NSDictionary *) [extendedData objectForKey:@"LatLonBox"];
double north = [[latLngBox objectForKey:@"north"] doubleValue];
double south = [[latLngBox objectForKey:@"south"] doubleValue];
double east = [[latLngBox objectForKey:@"east"] doubleValue];
double west = [[latLngBox objectForKey:@"west"] doubleValue];
// DEBUG
//NSLog(@"Parsed Bounding Box: ne = (%g,%g), sw = (%g,%g)", north, east, south, west);
MKCoordinateSpan span = MKCoordinateSpanMake(fabs(north - south), fabs(east - west));
MKCoordinateRegion region = MKCoordinateRegionMake(location, span);
[self.mapView setRegion:region animated:YES];
If all you want to do is generate a MKCoordinateRegion, you shouldn't need to know anything at all about zoom level. Just create a coordinate region using the width and height of the LatLonBox.
MKCoordinateSpan span = MKCoordinateSpanMake(fabs(north - south), fabs(east - west));
MKCoordinateRegion region = MKCoordinateRegionMake(location, span);
[self.mapView setRegion:region animated:YES];
精彩评论