Google Maps iOS SDK: How do I get accurate latitude and longitude coordinates from a camera's visibleRegion?

↘锁芯ラ 提交于 2020-01-19 14:10:36

问题


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:

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:

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?


回答1:


To get the bounding latitude and longitude you have to do the following steps:

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;

Best regards Robert




回答2:


I saw your issue here. Hope that they fix this issue in next update.

But now we can take the real visible region like this:

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

Drawing polyline method:

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

It works even for map with non-default bearing and angle.




回答3:


The solution is to download the latest version of the SDK (1.2 at the time of this writing) as the issue has been fixed.

From the 1.2 release notes:

Resolved Issues: - visibleRegion now reports correctly sized region on Retina devices

Download here.




回答4:


Looks like the latitude and longitude coordinates you are printing out and manually plotting are possibly off a bit / being truncated. %f defaults to only print out to 6 decimal places.

Here's a related question that might help:

How to print a double with full precision on iOS?




回答5:


Maybe you give the location Manager the wrong accuracy..

Try to increase it:

locationMgr.desiredAccuracy = kCLLocationAccuracyBest;

The battery is draining moreyy



来源:https://stackoverflow.com/questions/15317359/google-maps-ios-sdk-how-do-i-get-accurate-latitude-and-longitude-coordinates-fr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!