THIS CONTENT DOWNLOAD SHORTLY

Objective

My objective in this post would be to explain how you can add Google Maps SDK to implement features like show specific location, show route from one place to another, show user’s current location, show user’s updated location, etc.

 

This tutorial gives you brief idea about how to implement Google Maps SDK for iOS in your iOS Applications.

You Will Get Final Output:
Output

 

Step 1 Introduction

Google Maps SDK for iOS allows us to add maps to our iOS applications. Google Maps SDK automatically handles the Google Maps, and also response to the user’s activities such as

  • Tap
  • movement
  • zoom, etc.

Google Maps SDK also allows us to get user’s current location. We can get the location on the map by latitude and longitude of the place.

Now I will teach you how to integrate Google Maps SDK in iOS application using Swift. First of all, you need API Key for Google Maps. For that you need a Google Account.

 

Step 2 Get API Key

To use the Google Maps SDK, we need an API key.
API key is a key through which we can make calls to Google API from an iOS Application.
Follow these steps to get API Key for your app:

  1. Sign into Developers Console using your Google Account.
  2. Create Project if you haven’t created it yet, else go to Step 3.

    create-a-new-project

    • Add name of the project.
    • Click on Create button.
  3. Select the project by clicking on Select a project button available on Top-Right Corner.

    select-a-project

  4. Click on Enable and Manage APIs.

    enable-and-manage-apis

  5. It will list all the Google APIs. Select Google Maps SDK for iOS listed in Google Maps API.

    google-apis

  6. It will prompt to Enable the API. Click on Enable API.

    enable-api

  7. Your Google Maps SDK now enabled. But you can’t use it before creating credentials. Click on Go to Credentials to create credentials.

    google-maps-sdk-for-ios

  8. Click on New Credentials.

    new-api-credentials

    A drop-down box appears by clicking on New Credentials. Click on API Key.

    select-credential

  9. Click on iOS Key.

    create-a-new-key

  10. It will ask the name of the iOS API Key and Bundle Identifier of the iOS Application.

    create-ios-api-key

    • Add name of the iOS API Key
    • Add Bundle Identifiers of the iOS Applications. You can add multiple Bundle Identifiers in this section. It will accept request from iOS Applications with any of the Bundle Identifiers listed here.
    • When you are ready with all configurations, click on Create button.
  11. It will create API Key and display on the screen.

    api-key

    Copy this API Key and store it. You will need this API Key to use Google Maps SDK in your iOS Application.

I have done with all setup for API Key.

 

Step 3 Project Configuration

  1. Now create a new Xcode project. If you don't have any idea about how to create project please refer following link. http://www.theappguruz.com/blog/basic-of-creating-xcode-project-in-ios
  2. To integrate Google Maps SDK for iOS, you need to get GoogleMap framework. You can get Google Maps SDK by this link. https://developers.google.com/maps/documentation/business/mobile/ios/
  3. You can find GoogleMaps.framework in a downloaded folder. Drag GoogleMaps.framework to the project in Xcode.

    google-maps-framework

  4. A new window appears to choose the options for adding files. Check Copy items if needed checkbox if not checked already and click on Finish button.

    choose-options-to-add-file

  5. Now you can find GoogleMaps.framework framework in your project.
    • Right-click on it and select Show in finder.
    • Double click on GoogleMaps.framework in finder and select Resources folder.
    • You can find GoogleMaps.bundle.
    • Drag GoogleMaps.bundle to the Xcode project.
      google-maps-bundle
  6. A new window appears to choose the options for adding files. Now make sure Copy items if needed checkbox is unchecked and click on Finish.

    copy-items

    google-maps-demo-project

 

Step 4 Add Other Frameworks

Google Maps SDK needs several other frameworks. Such as

  • Accelerate.framework
  • AVFoundation.framework
  • CoreBluetooth.framework
  • CoreData.framework
  • CoreGraphics.framework
  • CoreLocation.framework
  • CoreText.framework
  • GLKit.framework
  • ImageIO.framework
  • libc++.dylib
  • libicucore.dylib
  • libz.dylib
  • OpenGLES.framework
  • QuartzCore.framework
  • Security.framework
  • SystemConfiguration.framework

To add these frameworks, select your project in Project Navigator and click on Build Phases. Expand Link Binary With Libraries and click on + button.

build-phases

Choose and add the above-listed frameworks.

choose-frameworks-and-libraries

 

Step 5 Add Bridging Header

As GoogleMaps.framework is available in Objective C, we need to add bridging header to our Swift project. By using bridging header, we can use Objective C code in our Swift application.

You can find more detail about Bridging Header by this link.

To add bridging header to the project

  1. Create new file by File >> New >> File from the menu to add temporary file.
  2. Select Objective-C File.
  3. Click Next.
  4. Add File Name.
  5. Click on Next.
  6. Click on Create.
  7. It will ask to configure Objective-C bridging header.
  8. Click on Create Bridging Header button.
    create-bridging-header

  9. It will create Objective-C bridging header file.
  10. Add following line of code to Bridging Header file.
    #import <GoogleMaps/GoogleMaps.h>
  11. Now delete the temporary created Objective-C file as we do not need that file any more.
 

Step 6 Display Map in our iOS Application

  1. Open the AppDelegate.swift file and use following lines of code in the application(_didFinishLaunchingWithOptions:) method.
    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.
        GMSServices.provideAPIKey("<YOUR_API_KEY_HERE>")
        return true
    }

    In this method, i would be using my API key to integrate Google Maps SDK in my iOS application.You have to set your iOS application API Key in

    <YOUR_API_KEY_HERE>.
  2. Now in ViewController.swift, add the following lines of code in viewDidLoad() method.
    override func viewDidLoad()
    {
        let camera: GMSCameraPosition = GMSCameraPosition.cameraWithLatitude(22.300000, longitude: 70.783300, zoom: 10.0)
        vwGMap = GMSMapView.mapWithFrame(self.view.frame, camera: camera)
        vwGMap.camera = camera
    }

    This method initializes the GMSCameraPosition. By default initial location will be displayed once map is loaded. For that you should get the latitude and longitude of the place.

 

Step 7 Get User’s Current Location

  1. Add your class to subclass of CLLocationManagerDelegate.
  2. Create variables of class CLLocationManager and GMSMapView.
    var locationManager = CLLocationManager()
    var vwGMap = GMSMapView()
  3. Add following lines of code in viewDidLoad() method of your ViewController.
    override func viewDidLoad()
    {
        super.viewDidLoad()
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyKilometer
    
        // A minimum distance a device must move before update event generated
        locationManager.distanceFilter = 500
    
        // Request permission to use location service
        locationManager.requestWhenInUseAuthorization()
    
        // Request permission to use location service when the app is run
        locationManager.requestAlwaysAuthorization()
    
        // Start the update of user's location
        locationManager.startUpdatingLocation()
    		
        // Add GMSMapView to current view
        self.view = vwGMap
    }
 

Step 8 CLLocationManagerDelegate methods

  1. When we request the permission by locationManager.requestWhenInUseAuthorization(), it will call locationManager(_didChangeAuthorizationStatus:). In which I have enabled myLocationEnabled to true. Now I am able to get the user’s current location.
    func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus)
    {
        if (status == CLAuthorizationStatus.AuthorizedWhenInUse)
        {
            vwGMap.myLocationEnabled = true
        }
    }
  2. Now locationManager(_didUpdateLocations) method is called at specific interval.
    func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
    {
        let newLocation = locations.last
        vwGMap.camera = GMSCameraPosition.cameraWithTarget(newLocation!.coordinate, zoom: 15.0)
        vwGMap.settings.myLocationButton = true
        self.view = self.vwGMap
    }
    In this method, we can get the updated locations. I have set the camera with the target location.
 

Step 9 Create marker and set the location

We can also set marker at the user’s current location. For that you need a variable of GMSMarker variable.

let marker = GMSMarker()

I would be using following lines of code to set the marker at the specified location.

// Create marker and set location
marker.position = CLLocationCoordinate2DMake(newLocation!.coordinate.latitude, newLocation!.coordinate.longitude)
marker.map = self.vwGMap

Add this code to locationManager(_didUpdateLocations) method. Now user will get the marker at his/her current location.

set-location

I hope you will find this blog post helpful while working with Google Maps Mobile SDK in iOS Swift. Let me know in comment if you have any questions regarding Google Maps Mobile SDK in iOS Swift. Please put a comment here and we will get back to you ASAP.

Got an Idea of iPhone App Development? What are you still waiting for? Contact us now and see the Idea live soon. Our company has been named as one of the best iPhone App Development Company in India.

I am 2D Game developer and iOS Developer with an aspiration of learning new technology and creating a bright future in Information Technology.

face mask Belial The Demon Headgear Pocket Staff Magic