Search code examples
objective-cgoogle-maps-sdk-ios

-convertPoint:toCoordinateFromView: method for Google Maps SDK for iOS


How do I implement the MapKit method

[googleMapView convertPoint:nePoint toCoordinateFromView:pinView];

in Google Maps SDK for iOS?


Solution

  • You could use something like this:

    GMSMapView* mapView = ...;
    CGPoint point = ...;
    ...
    CLLocationCoordinate2D coordinate = 
        [mapView.projection coordinateForPoint: point];
    

    In this case point should be relative to the map view.

    If you have a point relative to another view (eg pinView in your case), you'd need to convert that into a point relative to the map view first. You could do that with the following:

    UIView* pinView = ...;
    CGPoint pinViewPoint = ...;
    ...
    CGPoint mapViewPoint = [pinView convertPoint: pinViewPoint toView: mapView];
    

    So combining it all together:

    GMSMapView* mapView = ...;
    UIView* pinView = ...;
    CGPoint pinViewPoint = ...;
    ...
    CGPoint mapViewPoint = [pinView convertPoint: pinViewPoint toView: mapView];
    CLLocationCoordinate2D coordinate = 
        [mapView.projection coordinateForPoint: mapViewPoint];