我熟悉Google Maps API,现在我正在尝试学习iOS MapKit库。我有一个iPhone应用程序,它使用输入字符串并使用Google Maps地理编码器服务对其进行地理编码。我还想为新地图设置合适的缩放级别,但我不太清楚如何做到这一点。
阅读后,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];
答案 0 :(得分:1)
如果您只想生成MKCoordinateRegion,则根本不需要了解缩放级别。只需使用LatLonBox的宽度和高度创建一个坐标区域。
MKCoordinateSpan span = MKCoordinateSpanMake(fabs(north - south), fabs(east - west));
MKCoordinateRegion region = MKCoordinateRegionMake(location, span);
[self.mapView setRegion:region animated:YES];