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

9

编辑:这是一个针对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).
 */

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

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

我应用程序当前的投影

上述 NSLog 的输出给了我以下结果:

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

当我使用这个工具绘制这些坐标时,我得到了一个似乎不正确的投影:

actual projection

这些坐标在应用程序启动时保持一致,这让我相信我要么一直做错了什么,要么我误解了visibleRegion是什么,或者我发现了一个bug。有没有人愿意帮助我弄清楚哪一个?

卡在同一个问题上了。如果找到任何解决方案,请告诉我。谢谢。 - Mangesh
5个回答

12

要获取边界纬度和经度,您需要执行以下步骤:

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;

敬祝商祺 罗伯特


我很抱歉,我得到了相同的结果。 - andybons

7
我看到了你的问题这里。希望他们能在下一个更新中解决这个问题。
但是现在我们可以像这样获取真实的可见区域:
    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];
}

它甚至适用于具有非默认方位和角度的地图。

这段代码适用于1.3 SDK版本。获取新版本的参考文献:https://developers.google.com/maps/documentation/ios/reference/index - kaspartus

2
解决方案是下载最新版本的SDK(本文撰写时为1.2),因为问题已经得到解决。
从1.2版发布说明可以看出:

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

在此处下载 here

1

看起来你打印并手动绘制的纬度和经度坐标可能有些偏差/被截断了。 %f 默认只打印到小数点后6位。

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

如何在iOS上以完整精度打印double?


不行。即使使用NSNumber增加精度进行以下投影:http://imgur.com/sQBUjSi 我得到了这个输出:http://imgur.com/ZW5oB1I - andybons
更新了问题以反映您的精度建议。谢谢。 - andybons

-3
也许您给了位置管理器错误的精度..
尝试增加它:
locationMgr.desiredAccuracy = kCLLocationAccuracyBest;

电池的电量正在更快地消耗


我不这么认为。SDK会处理那个。 - andybons

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接