9

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?

David Barnard
  • 138
  • 1
  • 6

1 Answers1

21

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.

Community
  • 1
  • 1
Brett
  • 2,399
  • 1
  • 17
  • 23
  • Hi Brett, is it a good idea to allocate the GMSMapView in viewDidAppear? Or maybe it would be best to keep the map creation in loadView, but push the camera update to viewDidAppear (or perhaps viewWillAppear)? – Saxon Druce Oct 25 '13 at 03:38
  • I thought about doing this, but never tried it. I don't know why Google uses (void)loadView, but that's something I can look into outside of this question. Thanks, Brett! – David Barnard Oct 25 '13 at 18:32
  • Where did we use loadView? I'm happy to update published sample code as required =) – Brett Oct 28 '13 at 06:46
  • @Brett: The Getting Started doco uses loadView: https://developers.google.com/maps/documentation/ios/start#add_a_map – Saxon Druce Oct 28 '13 at 08:41