Search code examples
iosgoogle-maps-sdk-ios

Google Maps in MKMapView-type object


I have Google Maps working in my app by following the instructions on the 'Getting Started ' page here https://developers.google.com/maps/documentation/ios/start

The code example that gets you up and running (post frameworks and API keys etc) is below.

My problem is that as an Xcode noob, I want to know how to confine the map in an object on my view controller like in the MKMapView.

I am assuming that this line

@implementation YourViewController {
  GMSMapView *mapView_;
}

is the way that this code programmatically creates the map over the whole view controller?

How can I put it in something like the MKMapView?

#import "YourViewController.h"
#import <GoogleMaps/GoogleMaps.h>

@implementation YourViewController {
  GMSMapView *mapView_;
}

- (void)viewDidLoad {
  // 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_;

  // Creates a marker in the center of the map.
  GMSMarker *marker = [[GMSMarker alloc] init];
  marker.position = CLLocationCoordinate2DMake(-33.86, 151.20);
  marker.title = @"Sydney";
  marker.snippet = @"Australia";
  marker.map = mapView_;
}

@end

Solution

  • This is the line which makes the map take up the entire view, ie the root view is just the map:

    self.view = mapView_;
    

    Instead of doing this, create some other view, and then add the map view to it. It will depend on whether you're creating your views manually, or using a xib/storyboard.

    [otherView addSubview: mapView_];
    

    You will also need to change this line:

    mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera];
    

    Setting the frame to CGRectZero will resize the map to fill the screen, only if the map view is the root view. You will instead need to specify the size of the frame that you want.