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

前端 未结 5 1556
野的像风
野的像风 2020-12-11 04:20

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 long

相关标签:
5条回答
  • 2020-12-11 04:38

    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?

    0 讨论(0)
  • 2020-12-11 04:45

    Maybe you give the location Manager the wrong accuracy..

    Try to increase it:

    locationMgr.desiredAccuracy = kCLLocationAccuracyBest;
    

    The battery is draining moreyy

    0 讨论(0)
  • 2020-12-11 04:50

    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.

    0 讨论(0)
  • 2020-12-11 04:51

    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.

    0 讨论(0)
  • 2020-12-11 04:55

    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

    0 讨论(0)
提交回复
热议问题