【问题标题】:Equivalent Objective-C code for Google Maps GMap2.getBoundsZoomLevel(bounds)?Google Maps GMap2.getBoundsZoomLevel(bounds) 的等效 Objective-C 代码?
【发布时间】: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


    【解决方案1】:

    如果您只想生成一个 MKCoordinateRegion,那么您根本不需要了解任何有关缩放级别的信息。只需使用 LatLonBox 的宽度和高度创建一个坐标区域。

    MKCoordinateSpan span = MKCoordinateSpanMake(fabs(north - south), fabs(east - west));
    MKCoordinateRegion region = MKCoordinateRegionMake(location, span);
    [self.mapView setRegion:region animated:YES];
    

    【讨论】:

    • 该代码似乎为以下变量生成了无效区域。位置:(-93.6256,42.0234) 和边界框:ne = (42.0565,-93.5616), sw = (41.9902,-93.6897)。我收到“NSInvalidArgumentException”,原因:“无效区域 <-93.62562200 span:>
    猜你喜欢
    • 2014-03-21
    • 2015-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多