Google Maps iOS SDK:如何从相机的visibleRegion获取准确的纬度和经度坐标?

时间:2013-03-10 00:02:34

标签: google-maps google-maps-sdk-ios

编辑:现在这是this SDK

的确认错误

我正在使用Google Maps for iOS SDK的1.1.1.2311版本,我希望在屏幕上找到可见地图的边界纬度和经度坐标。

我正在使用以下代码告诉我当前的预测是什么:

NSLog(@"\n%@,%@\n%@,%@\n%@,%@\n%@,%@\n",
    [NSNumber numberWithDouble:mapView.projection.visibleRegion.farLeft.latitude],
    [NSNumber numberWithDouble:mapView.projection.visibleRegion.farLeft.longitude],
    [NSNumber numberWithDouble:mapView.projection.visibleRegion.farRight.latitude],
    [NSNumber numberWithDouble:mapView.projection.visibleRegion.farRight.longitude],
    [NSNumber numberWithDouble:mapView.projection.visibleRegion.nearLeft.latitude],
    [NSNumber numberWithDouble:mapView.projection.visibleRegion.nearLeft.longitude],
    [NSNumber numberWithDouble:mapView.projection.visibleRegion.nearRight.latitude],
    [NSNumber numberWithDouble:mapView.projection.visibleRegion.nearRight.longitude]);

通过阅读标题,相机移动时可能无法更新。足够公平......

/**
 * The GMSProjection currently used by this GMSMapView. This is a snapshot of
 * the current projection, and will not automatically update when the camera
 * moves. The projection may be nil while the render is not running (if the map
 * is not yet part of your UI, or is part of a hidden UIViewController, or you
 * have called stopRendering).
 */

但是,每次调用委托方法时它似乎都会更新,所以我试图绘制坐标以测试它们......

对于我手机上的以下内容:

my app's current projection

上面的NSLog的输出给出了以下内容:

37.34209003645947,-122.0382353290915
37.34209003645947,-122.010769508779
37.30332095984257,-122.0382353290915
37.30332095984257,-122.010769508779

使用this绘制这些坐标时,我得到了一个似乎关闭的投影:

actual projection

这些坐标在应用程序发布时是一致的,这使我相信我要么一直做错了,我误解了visibleRegion是什么,或者我发现了一个错误。有人想帮我弄清楚它是哪一个吗?

5 个答案:

答案 0 :(得分:11)

要获得边界纬度和经度,您必须执行以下步骤:

GMSCoordinateBounds *bounds = [[GMSCoordinateBounds alloc] initWithRegion:self.googleMapsView.projection.visibleRegion];

CLLocationCoordinate2D northEast = bounds.northEast;
CLLocationCoordinate2D northWest = CLLocationCoordinate2DMake(bounds.northEast.latitude, bounds.southWest.longitude);
CLLocationCoordinate2D southEast = CLLocationCoordinate2DMake(bounds.southWest.latitude, bounds.northEast.longitude);
CLLocationCoordinate2D southWest = bounds.southWest;

祝你好运 罗伯特

答案 1 :(得分:6)

我看到了您的问题here。希望他们在下次更新中解决这个问题。

但现在我们可以采取这样的真实可见区域:

    CGPoint topLeftPoint = CGPointMake(0, 0);
    CLLocationCoordinate2D topLeftLocation =
    [_mapView.projection coordinateForPoint: topLeftPoint];

    CGPoint bottomRightPoint =
    CGPointMake(_mapView.frame.size.width, _mapView.frame.size.height);
    CLLocationCoordinate2D bottomRightLocation =
    [_mapView.projection coordinateForPoint: bottomRightPoint];

    CGPoint topRightPoint = CGPointMake(_mapView.frame.size.width, 0);
    CLLocationCoordinate2D topRightLocation =
    [_mapView.projection coordinateForPoint: topRightPoint];

    CGPoint bottomLeftPoint =
    CGPointMake(0, _mapView.frame.size.height);
    CLLocationCoordinate2D bottomLeftLocation =
    [_mapView.projection coordinateForPoint: bottomLeftPoint];

    GMSVisibleRegion realVisibleRegion;
    realVisibleRegion.farLeft = topLeftLocation;
    realVisibleRegion.farRight = topRightLocation;
    realVisibleRegion.nearLeft = bottomLeftLocation;
    realVisibleRegion.nearRight = bottomRightLocation;

    [self drawPolylineWithGMSVisibleRegion:realVisibleRegion color:[UIColor redColor] width:10.0f forMap:mapView];

绘制折线方法:

- (void)drawPolylineWithGMSVisibleRegion:(GMSVisibleRegion)visibleRegion
                               color:(UIColor*)color
                               width:(double)width
                              forMap:(GMSMapView*)mapView{
    GMSPolylineOptions *rectangle = [GMSPolylineOptions options];
    rectangle.color = color;
    rectangle.width = width;
    GMSMutablePath *path = [GMSMutablePath path];
    [path addCoordinate:visibleRegion.nearRight];
    [path addCoordinate:visibleRegion.nearLeft];
    [path addCoordinate:visibleRegion.farLeft];
    [path addCoordinate:visibleRegion.farRight];
    [path addCoordinate:visibleRegion.nearRight];
    rectangle.path = path;
    [mapView addPolylineWithOptions:rectangle];
}

即使对于非默认方位和角度的地图也适用。

答案 2 :(得分:2)

解决方案是在问题得到修复后下载最新版本的SDK(撰写本文时为1.2)。

从1.2发行说明:

  

已解决的问题:      - visibleRegion现在报告Retina设备上正确尺寸的区域

下载here

答案 3 :(得分:1)

看起来您正在打印的纬度和经度坐标以及手动绘图可能会有点/被截断。 %f默认只打印到6位小数。

这是一个可能有帮助的相关问题:

How to print a double with full precision on iOS?

答案 4 :(得分:-3)

也许你给位置经理一个错误的准确性..

尝试增加它:

locationMgr.desiredAccuracy = kCLLocationAccuracyBest;

电池正在耗尽