Google Maps SDK and Places API integration in iOS

Swarnendu De December 9, 2014

Introduction

Google Maps SDK integration in iOS apps has nowadays become pretty common due to its flexible features and small size. This tutorial will walk you through the whole process in step by step manner about how to integrate the Google Map.

Getting the Google Maps SDK

Get the SDK for Google Maps from Google Developers Site.

Obtaining the API Key for your SDK

You can obtain a key for your app in the Google APIs Console.

Steps for creating the API key for Google Map integration:

  • Create a project in the Google APIs Console.
  • Select the Services pane in your API project, and enable the Google Maps SDK for iOS. This will display Google Maps Terms of Service which has to be accepted in order to make the SDK function properly.
  • Select the API Access pane in the console, and click Create new ‘iOS key’.
  • Enter one or more bundle identifiers as listed in your application’s Project.plist file.
  • Click Create.
  • In the API Access page, locate the section Key for iOS apps (with bundle identifiers) and note or copy the API key.

Steps for creating the API key for Places API:

For using places API in your project, you will need a ‘browser key’ instead of an ‘iOS key’. To get the browser key follow the first two steps mentioned for obtaining the API key for Google Map Integration . After that follow the steps mentioned below:

  • Select the API Access pane in the console and click create new ‘browser key’.
  • A dialogue box will open; leave the box blank and click on Create.
  • In the API Access page, locate the section Key for iOS apps (with bundle identifiers) and note or copy the API key.

Adding the Google Maps SDK into your iOS Project

The Google Maps SDK for iOS is packaged as a static framework with an included resource bundle. Before you can add a map to your application, you will need to add the framework to your project, and configure your build settings in Xcode. These instructions assume an installation for a new project. If you are working with an existing project, you may not have to follow the steps exactly as described.

  1. Launch Xcode and open the project.
  2. Open the Info.plist file of your project and add two keys to the file:
    • NSLocationWhenInUseUsageDescription with description you want to display for the permission alert for tracking user location.
    • NSLocationAlwaysUsageDescription with description you want to display for the permission alert for tracking user location.
  3. Drag the GoogleMaps.Framework bundle to the Frameworks group of your project. When prompted, select Copy items into destination group’s folder.
  4. Right-click GoogleMaps.Framework in your project, and select Show In Finder.
  5. Drag the GoogleMaps.bundle from the Resources folder to your project. We suggest putting it into the Frameworks group. When prompted, ensure Copy items into destination group’s folder is not selected.
  6. Select your project from the Project Navigator, and choose your application’s target.
  7. Open the Build Phases tab, and within Link Binary with Libraries, add the following frameworks:
    • AVFoundation.framework
    • CoreData.framework
    • CoreLocation.framework
    • CoreText.framework
    • GLKit.framework
    • ImageIO.framework
    • libc++.dylib
    • libicucore.dylib
    • libz.dylib
    • OpenGLES.framework
    • QuartzCore.framework
    • SystemConfiguration.framework
  8. Choose your project, rather than a specific target, and open the Build Settings tab. In the Other Linker Flags section, add -ObjC. If these settings are not visible, change the filter in the Build Settings bar from Basic to All.
  9. Finally, add your API key to your AppDelegate in the following ways :
    • In AppDelegate.h add #import <GoogleMaps/GoogleMaps.h>
    • In application:didFinishLaunchingWithOptions method add the following statement replacing the API_KEY with the API Key you obtained by creating iOS Key.
      [GMSServices provideAPIKey:@"API_KEY”];

       


Map-Blog-Pic (2)-1

Show Map in Your View Controller

After adding the key in your app delegate all you are left with is showing map in your view controller. For that in your ViewController.m and add following

#import<GoogleMaps/GoogleMaps.h>

Then add the following lines of codes in your ViewController.m under ViewDidLoad() method:

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


//Creates a marker at kolkata location.
  GMSMarker *marker = [[GMSMarker alloc] init];
  marker.position = CLLocationCoordinate2DMake(22.595769, 88.263639);
  marker.title = @"Howrah";
  marker.snippet = @"Kolkata";
  marker.map = mapView_;

After that you can do whatever you wish with the Google Map with the help of Google Map Object reference.

If you wish to play with drawing objects like markers, tiles etc. refer to the link Google map markers reference. Google Places API provide search facility with respect to a service. Those services can be seen listed in Places API Supported types .

For having this facility in your app the only thing you have to do is to make an API call using the Browser key you obtained while configuring your project on Google API console. The sample API for nearby search is given below:

https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=-33.8670522,151.1957362&radius=500&keyword=atm&key=BROWSER_KEY

The obtained JSON can be parsed and you can show the search results on the map using the location of the objects obtained in the JSON. For more details on how to use the Places API, refer to the link Google Places Search Request.

For Google Maps integration sample Code and tracking location using maps, you can refer to this sample project on GitHub.

For tracking location using device GPS, you can refer to this sample project on GitHub.

This is it. Hope it helps. Feel free to comment if you face any problem while integrating Google Maps SDK. Any suggestion for improvement will be appreciated.