我对Google Maps API很熟悉,现在我正在尝试学习iOS MapKit库。我有一个iPhone应用程序,它接受一个输入字符串,并使用Google Maps geocoder服务对其进行地理编码。我还想为新地图设置一个适当的缩放级别,但我不太确定如何做。
Determining zoom level from single LatLong in Google Maps API
我的计划是解析来自Google Maps API的JSON响应并提取ExtendedData字段。
"ExtendedData":{
"LatLonBox":{
"north":34.13919,
"south":34.067018,
"east":-118.38971,
"west":-118.442796
}
然后我想使用MapKit setRegion函数相应地设置我的映射的边界。
我开始设计一个函数来做这个,但是我对逻辑有点迷茫。。。
- (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];
}
或者,我想我可以使用地理代码结果的精度来设置一种默认的缩放级别。我只是不知道如何计算不同结果的等效默认缩放级别。
见
https://code.google.com/apis/maps/documentation/geocoding/v2/#GeocodingAccuracy
// 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];