business@innofied.com

US Toll-Free - +1 866 941 5117

Top
Google-Maps-SDK-integration

Google Maps SDK and Places API integration in iOS

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.

Manish Kumar

Manish Kumar is an iOS developer at Kolkata, who enjoys building innovative applications. Besides he also loves playing football and listening to songs. He loves challenges and sharing his work in open source community. Lots of iOS objective C development work is going on in Innofied. You can reach us at contact@innofied.com

  • Arun

    Could be more elaborate…. and there is no need to import those many frameworks

    • Manish Kumar

      Which part should be elaborated?

      • joaquin102

        Pretty much everyhing.. You could show a complete example, or a step by step code.. etc

        • Manish Kumar

          @joaquin102:disqus Thank you for your feedback. Very soon I will upload a sample project with all required features

  • hello sir i am working on map kit framework in ios. i want to show all the nearest famous places like hotels, restro, and bar clubs from the current user location. please help me to do that.

  • Jaydeep Rudani

    how can i get current location’s longitude and lattitude

  • Sambit behera

    I want to develop one app in which u can find ur current location and if u given any direction then widin 10k.m radius u fine all the hotel. Am trying google place api.bt i can’t find any solution,plz give any sample tutorial.

    Regards,

    Sambit

  • Manish Kumar

    Hello Sambit,
    I didn’t understand this statement “and if u given any direction”. Please elaborate.

  • Joseph

    Hi Manish, I follow your instructions by adding GoogleMaps into my app from the start until the end but it crashes when I tap GoogleMap icon. I am using iOS 9.2 (the latest one). The error says “CoreData: Failed to load optimised model at path”. I google but it cannot solve my problem. Please assist me.

    • Manish Kumar

      Have you enabled Core Data for your project?

      • Joseph

        No, I haven’t. I didn’t tick “Core Data” when I create new project. Is it possible for me to enable Core Data even although I am using existing project without Core Data.

        • Joseph

          No need to reply me because I have solved it. Thanks a lot!

  • Snehal Lokhande

    I am not able to download the sample code of tracking location using GPS device. It says Access Denied!
    Would you please help me by sharing that sample code with me?