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

and*_*ons 8 google-maps objective-c google-maps-sdk-ios

编辑:这是 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]);
Run Code Online (Sandbox Code Playgroud)

从阅读标题时,似乎相机移动时可能无法更新.很公平...

/**
 * 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).
 */
Run Code Online (Sandbox Code Playgroud)

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

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

我的应用程序的当前预测

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

37.34209003645947,-122.0382353290915
37.34209003645947,-122.010769508779
37.30332095984257,-122.0382353290915
37.30332095984257,-122.010769508779
Run Code Online (Sandbox Code Playgroud)

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

实际投影

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

Rob*_*ndl 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;
Run Code Online (Sandbox Code Playgroud)

最好的问候Robert


kas*_*tus 6

我在这里看到你的问题.希望他们在下次更新中解决此问题.

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

    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];
Run Code Online (Sandbox Code Playgroud)

绘制折线方法:

- (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];
}
Run Code Online (Sandbox Code Playgroud)

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


and*_*ons 2

解决方案是下载最新版本的 SDK(撰写本文时为 1.2),因为该问题已得到解决。

来自 1.2 发行说明:

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

在这里下载。