Add Google Maps in Swift

To add Google Maps in Swift within your project, you will first need to download the Google Maps SDK for iOS.

You can either install the SDK using CocoaPods, or manually by downloading the zip file and adding the files into specific locations within your project in Xcode.

To install using CocoaPods, ensure that you have installed CocoaPods itself by running the following command.

$ sudo gem install cocoapods

This will install the cocoapods gem and its dependencies.

Now within your Xcode project, create a Podfile file, this will define your project’s dependencies, similar to how a composer.json file is used in PHP projects.

This can be done by cd‘ing into your project using your computer’s terminal application, and running the cocoapods init command.

$ pod init

This will create a Podfile. Within this file, add the GoogleMaps dependency. The Podfile might look similar to the below.

target '[YOUR_PROJECT_NAME]' do
  # Pods for [YOUR_PROJECT_NAME]
  pod 'GoogleMaps'
end

Save the file, and within the same directory, run the pod install command to install the cocoapods repo.

If this is your first time running the install command, please note that it might take a while to complete!

When the command has finished running, you should see the following output within terminal that looks like the following:

Setup completed
Analyzing dependencies
Downloading dependencies
Installing GoogleMaps (2.3.0)
Generating Pods project
Integrating client project

[!] Please close any current Xcode sessions and use `[YOUR_PROJECT_NAME].xcworkspace` for this project from now on.

As the last lines suggest, close your Xcode project and re-open it by clicking on the [YOUR_PROJECT_NAME].xcworkspace file.

Let’s assume you have a simple single view application. Amend the AppDelegate.swift file and import the GoogleMaps library just underneath the UIKit import.

Within the application() function, include your Google API key here using provideAPIKey() from the GMSServices library.

import UIKit
import GoogleMaps

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.

        // Add the Google API Key
        GMSServices.provideAPIKey("YOUR_API_KEY_HERE")
        return true
    }
}

To obtain the API key, head to the Get API Key link and enable the Google Maps SDK for iOS.

And finally, within your ViewController.swift file

import UIKit
import GoogleMaps

class YourViewController: UIViewController {

  // You don't need to modify the default init(nibName:bundle:) method.

  override func loadView() {
    // 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)
    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
  }
}

Save and build your project within Xcode, and you should notice that your view now shows a map!

Add Google Maps in Swift