Fit bounds not working as expected

David Barnard picture David Barnard · Oct 24, 2013 · Viewed 7.2k times · Source

I'm going through the Google Maps SDK for iOS Getting Started pages to find out how to zoom and center the view on a given bounds. Code for this is provided in Build a GMSCameraPosition, which mentions "It's sometimes useful to move the camera such that an entire area of interest is visible at the greatest possible zoom level."

This wording is similar to another possible approach via a GMSCameraUpdate, "Returns a GMSCameraUpdate that transforms the camera such that the specified bounds are centered on screen at the greatest possible zoom level."

The code below is taken straight from the two links in the Getting Started pages -- adapted slightly to provide meaningful screenshots; adaptations have no affect on the actual result.

- (void)loadView
{
// Create a GMSCameraPosition that tells the map to display the
// coordinate -33.86,151.20 at zoom level 6.
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.86
                                                        longitude:151.20
                                                             zoom:6];
mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera];
mapView_.myLocationEnabled = YES;
self.view = mapView_;

CLLocationCoordinate2D vancouver = CLLocationCoordinate2DMake(49.26, -123.11);
CLLocationCoordinate2D calgary = CLLocationCoordinate2DMake(51.05, -114.05);

GMSMarker *vancouverMarker = [[GMSMarker alloc] init];
vancouverMarker.position = vancouver;
vancouverMarker.title = @"Vancouver";
vancouverMarker.map = mapView_;

GMSMarker *calgaryMarker = [[GMSMarker alloc] init];
calgaryMarker.position = calgary;
calgaryMarker.title = @"Calgary";
calgaryMarker.map = mapView_;

GMSCoordinateBounds *bounds =
[[GMSCoordinateBounds alloc] initWithCoordinate:vancouver coordinate:calgary];

[mapView_ moveCamera:[GMSCameraUpdate fitBounds:bounds]];
//These last two lines are expected to give the same result as the above line
//camera = [mapView_ cameraForBounds:bounds insets:UIEdgeInsetsZero];
//mapView_.camera = camera;
}

However, the expected result does not match the actual result.

Expected result
expected

Actual result
actual

Perhaps I am confused about the meaning of "greatest possible zoom level." I assume it means as closely zoomed in as possible, not zoomed out. Either way, what am I doing wrong or is this a bug?

Answer

Brett picture Brett · Oct 25, 2013

Here is a slight change to your code that makes it work as expected:

- (void)viewDidAppear:(BOOL)animated
{
  [super viewDidAppear:animated];

  // Create a GMSCameraPosition that tells the map to display the
  // coordinate -33.86,151.20 at zoom level 6.
  GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.86
                                                      longitude:151.20
                                                           zoom:6];
  mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera];
  mapView_.myLocationEnabled = YES;
  self.view = mapView_;

  CLLocationCoordinate2D vancouver = CLLocationCoordinate2DMake(49.26, -123.11);
  CLLocationCoordinate2D calgary = CLLocationCoordinate2DMake(51.05, -114.05);

  GMSMarker *vancouverMarker = [[GMSMarker alloc] init];
  vancouverMarker.position = vancouver;
  vancouverMarker.title = @"Vancouver";
  vancouverMarker.map = mapView_;

  GMSMarker *calgaryMarker = [[GMSMarker alloc] init];
  calgaryMarker.position = calgary;
  calgaryMarker.title = @"Calgary";
  calgaryMarker.map = mapView_;

  GMSCoordinateBounds *bounds =
  [[GMSCoordinateBounds alloc] initWithCoordinate:vancouver coordinate:calgary];

  [mapView_ moveCamera:[GMSCameraUpdate fitBounds:bounds]];
  //These last two lines are expected to give the same result as the above line
  //camera = [mapView_ cameraForBounds:bounds insets:UIEdgeInsetsZero];
  //mapView_.camera = camera;
}

See Difference between viewDidLoad and viewDidAppear for an explanation.