8

I am developing iOS application including GoogleMaps. And implementation process of GoogleMaps SDK for iOS was completed yet.

But I want to know how to calculate GMSCamera zoom for showing whole route on Map.

In Apple Map, we use span, maybe. But GoogleMaps SDK doesn't have span.

Please give me advice.

JP Illanes
  • 3,665
  • 39
  • 56

7 Answers7

18

Actually in the current version of the SDK you can use fitBounds:

GMSCoordinateBounds *bounds = [[GMSCoordinateBounds alloc] initWithPath:yourPath];
//There are several useful init methods for the GMSCoordinateBounds!
GMSCameraUpdate *update = [GMSCameraUpdate fitBounds:bounds];
[mapView_ moveCamera:update];

This way you let the math to Google, and I'm pretty sure they are really good at it.

JP Illanes
  • 3,665
  • 39
  • 56
3

Here is a function that may help as this calculates the zoomLevel from an MKMapView that is "Google Compatible"

- (NSInteger)getZoomLevel
{
        MKMapView *map = (MKMapView *)self.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;
}

If you can grab the coordinates and pass those in instead you should be ok.

Lee Armstrong
  • 11,420
  • 15
  • 74
  • 122
  • Is the math calculating the zoomscale right? Why I got a smaller zoom on google maps comparing to the MapKit map with the same longitudeDelta... – Jing Mar 04 '13 at 05:38
3

Lee's method is right, but the math is not right, based on google maps ios sdk document on zoom

Increasing the zoom level by 1 doubles the width of the world on the screen. Hence at zoom level N, the width of the world is approximately 256 * 2^N, i.e., at zoom level 2, the whole world is approximately 1024 points wide. Note that the zoom level need not be an integer. The range of zoom levels permitted by the map depends on a number of factors including location, map type and screen size.

The math to calculate the zoom should be

zoom = log2(360 * mapView.bounds.size.width/ longitudeDelta) - 8;
Jing
  • 1,935
  • 3
  • 17
  • 26
  • 1
    Inverse problem solution is here: `longitudeDelta = 360*mapView.bounds.size.width/(1 << (zoom+8))` – k06a Aug 29 '14 at 16:04
1

I posted an answer to a similar question here:

How to setRegion with google maps sdk for iOS?

It uses a similar approach to Lee's answer: Convert the lat/lon to pixels, calculate a scale, then a zoom level.

Community
  • 1
  • 1
Saxon Druce
  • 17,406
  • 5
  • 50
  • 71
1

Swift:

//Create a path
let path = GMSMutablePath()

//for each point you need, add it to your path
let position = CLLocationCoordinate2DMake(latitude, longitude)
path.add(position)

//Update your mapView with path
let mapBounds = GMSCoordinateBounds(path: path)
let cameraUpdate = GMSCameraUpdate.fit(mapBounds)
mapView.moveCamera(cameraUpdate)
Shaked Sayag
  • 5,712
  • 2
  • 31
  • 38
0

Jing, your math is correct for longitudes, but for latitudes it has to be a bit more tricky. Mercator projection handles latitudes and longitudes differently. I have posted the correct code here: https://stackoverflow.com/a/16217785/2291425

Community
  • 1
  • 1
Evgeny Tanhilevich
  • 1,119
  • 1
  • 8
  • 17
0

Use GMSCoordinateBounds. You can fit the map using different parameters like cooodinates, bounds etc.

GMSCoordinateBounds *bounds = [[GMSCoordinateBounds alloc] initWithPath:path];

https://developers.google.com/maps/documentation/ios/reference/interface_g_m_s_coordinate_bounds#a583bf55d8dd8cd10eec3473688f9d788