UIChris
UIChris

Reputation: 621

-[GMSMapView region]: unrecognized selector - Issue with Google Maps SDK for iOS

I'm here because I have a problem with the Google Maps SDK for iOS.

My map is running properly, and everything works fine.

In my project, I'm trying to get a dynamic zoom between 2 markers, in order to see them in the same frame. So here is my code (I get it somewhere in stackoverflow too) :

- (NSInteger)getZoomLevel
{
    MKMapView *map = (MKMapView *)mapView_;
    CLLocationDegrees longitudeDelta = map.region.span.longitudeDelta;
    CGFloat mapWidthInPixels = map.bounds.size.width;
    double zoomScale = longitudeDelta * 85445659.44705395 * M_PI / (180.0 * mapWidthInPixels);
    double zoomer = 20 - log2(zoomScale);
    if ( zoomer < 0 ) zoomer = 0;
    return (NSInteger)zoomer;
}

And this is how I call it :

GMSCameraUpdate *zoomCamera = [GMSCameraUpdate zoomIn];
    [mapView_ animateWithCameraUpdate:zoomCamera];

    CLLocationCoordinate2D myPosition = CLLocationCoordinate2DMake(mapView_.myLocation.coordinate.latitude, mapView_.myLocation.coordinate.longitude);
    GMSCameraUpdate *myPositionCam = [GMSCameraUpdate setTarget:myPosition];
    [mapView_ animateToZoom:[self getZoomLevel]];
    [mapView_ animateWithCameraUpdate:myPositionCam];

The point is to see 2 markers in the same frame.

This is what I have when I try to run it :

-[GMSMapView region]: unrecognized selector sent to instance 0x210527e0

Any ideas ? Or if anyone get a best sample code to get the right zoom.

Thanks !! :)

Upvotes: 0

Views: 1477

Answers (2)

Saxon Druce
Saxon Druce

Reputation: 17624

The sample code which you got from How to calculate GMSCamera zoom was showing how it could be done with an MKMapView - the intention being to adapt it to the equivalent code for a GMSMapView (not using the code directly).

There's a more comprehensive answer (specifically for GMSMapView) here:

How to setRegion with google maps sdk for iOS?

However, both of those questions were asked before Google added a fitBounds method which should be able to be used instead of writing your own code:

https://developers.google.com/maps/documentation/ios/reference/interface_g_m_s_camera_update

I noticed though that somebody reported that fitBounds didn't work for them - so see how you go.

Upvotes: 2

Stunner
Stunner

Reputation: 12194

The problem is that you are accessing the region method of the GMSMapViewRegion which doesn't exist (map.region in line: CLLocationDegrees longitudeDelta = map.region.span.longitudeDelta;).

You are using code that is meant for Apple's map implementation in Google's which is incompatible.

Upvotes: 1

Related Questions