9

EDIT: This is now a confirmed bug with this SDK

I'm using version 1.1.1.2311 of the Google Maps for iOS SDK, and I'm looking to find the bounding latitude and longitude coordinates for the visible map on screen.

I'm using the following code to tell me what the current projection is:

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]);

From reading the headers, it seems that it may not be updated when the camera moves. Fair enough...

/**
 * 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).
 */

But, it appears to update each time the delegate method is called, so I attempted to plot the coordinates to test them...

For the following on my phone:

my app's current projection

The output of the NSLog from above gives me the following:

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

When plotting those coordinates using this I get a projection that seems off:

actual projection

These coordinates are consistent across app launches which leads me to believe that I'm either consistently doing something wrong, I'm misunderstanding what visibleRegion is, or I've discovered a bug. Anyone care to help me figure out which one it is?

4

5 回答 5

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;

最好的问候罗伯特

于 2013-03-10T23:56:52.027 回答
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];

绘制折线方法:

- (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];
}

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

于 2013-04-01T09:07:09.023 回答
2

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

从 1.2 发行说明:

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

在这里下载。

于 2013-04-18T04:51:15.377 回答
1

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

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

如何在 iOS 上以全精度打印双精度?

于 2013-03-12T19:33:42.030 回答
-3

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

尝试增加它:

locationMgr.desiredAccuracy = kCLLocationAccuracyBest;

电池越来越耗电了

于 2013-03-10T00:08:48.387 回答