Swift 3.0 Google Maps API
By: Isa Weaver
Tags: iOS, Swift, Google Maps

On the journey to become a better iOS developer one undoubtedly will have to use an API or Application Program Interface. The one being used today is the Google Maps API which basically allows a developer incorporate Google Maps service into their application. It saves the hassle of one having to develop an entirely new maps system. This will be a guide teaching one how to add Google Maps to their Swift 3 iOS project.

Step 1: Install the Google Maps Software Development Kit (SDK)

This part will be done using the Swift and Objective-C open source dependency manager CocoaPods. First one will need to install CocoaPods on their macOS if it is not already installed. To check if CocoaPods is installed on your OS run this command in your terminal.

pod --version

If the command returns ‘not found’ than CocoaPods is not installed on the OS, alternatively the command will return the version number. To install CocoaPods enter the following command in the terminal.

sudo gem install cocoapods

After which one will have CocoaPods installed on the OS. The next step is to open XCode and create a Single View Application. After that is created return to the terminal window to install the pod file in the project. One has to change the directory to the XCode project being worked on.

Shortcut for Changing Directory

After opening the terminal type:

cd "projectDirectoryName"

Then press enter. This can be done easily by holding command and space at the same time to bring up finder. Then type the name of the project and drag the project folder into the terminal so it automatically enters the project directory name. After which you create a pod file using the command:

pod init

Open the pod file in the XCode editor and add these lines of code:

target 'YOUR_APPLICATION_TARGET_NAME_HERE' do

pod 'GoogleMaps'

pod 'GooglePlaces'

end

After, save the pod file and return to the terminal with the directory location on the project. Then run the command:

pod install

When the pods have finished installing, close XCode and resume the session with the .xcworkspace for all continuous development in the project.

Step 2: Get a Google Maps API Key and Add to Application

This is the link to get the Google Maps API Key: https://developers.google.com/maps

After getting the API Key, open the XCode application and navigate to AppDelegate.swift add:

import GoogleMaps

Add the following to the application(_:didFinishLaunchingWithOptions:) method:

GMSServices.provideAPIKey("YOUR_API_KEY")

For the places API:

GMSPlacesClient.provideAPIKey("YOUR_API_KEY")

In the ViewController add this:

import UIKit import GoogleMaps class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() // Create a GMSCameraPosition that tells the map to display the // coordinate -33.86,151.20 at zoom level 6. let camera = GMSCameraPosition.camera(withLatitude: -33.86, longitude: 151.20, zoom: 6.0) let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera) mapView.isMyLocationEnabled = true view = mapView // Creates a marker in the center of the map. let marker = GMSMarker() marker.position = CLLocationCoordinate2D(latitude: -33.86, longitude: 151.20) marker.title = "Sydney" marker.snippet = "Australia" marker.map = mapView }

Step 3: Declare the URL schemes and Implementation

Declare the URL schemes used by the Google Maps API by adding these lines to the Info.plist file:

<key>LSApplicationQueriesSchemes</key>

<array>

<string>googlechromes</string>

<string>comgooglemaps</string>

</array>

Now its time for show and tell. Run the application.

Yay! Google Maps API

Please give feedback if this tutorial was helpful. Or ask questions if you want to know more.