【发布时间】:2011-05-02 15:24:05
【问题描述】:
我熟悉 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];
【问题讨论】:
标签: objective-c api google-maps mapkit geocoding