Google Map with iOS (Swift)

Mohd Danish Khan
Nov 3 · 2 min read

This about setting Google Map on iOS (Swift) project and fetch your current location.

Google Map in iOS with Current Location

STEPS:

1. GENERATE GOOGLE MAP SDK KEY

Use the google console link below to enable Google MAP SDK

https://console.developers.google.com/

2. SETUP SDK IN PROJECT

a. run “pod init” (without quotes) in your terminal with project root.

b. Add these pods in your podfile

pod ‘GoogleMaps’
pod ‘GooglePlaces’

c. run “pod install” (without quotes) in your teminal with project root

3. SETUP APPDELEGATE

import UIKit

import GoogleMaps

let googleApiKey = “your key here

@UIApplicationMain

class AppDelegate: UIResponder, UIApplicationDelegate {

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

// Override point for customization after application launch.

GMSServices.provideAPIKey(googleApiKey)

return true

}

4. SETUP UIVIEW WITH CLASS NAME GMSMAPView

Drag and drop UIView in your viewController in storyboard.

Change the class UIView to GMSMapView

Add the IBOutlet of this view in your code

@IBOutlet weak var mapView: GMSMapView!

5. SETUP LOCATION MANAGER

private let locationManager = CLLocationManager()

override func viewDidLoad() {

super.viewDidLoad()

// Do any additional setup after loading the view.

locationManager.delegate = self

locationManager.requestWhenInUseAuthorization()

}

6. SETUP CLLOCATION DELEGATE:

class ViewController: UIViewController, CLLocationManagerDelegate

func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {

guard status == .authorizedWhenInUse else {

return

}

// 4

locationManager.startUpdatingLocation()

//5

mapView.isMyLocationEnabled = true

mapView.settings.myLocationButton = true

}

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

guard let location = locations.first else {

return

}

mapView.camera = GMSCameraPosition(target: location.coordinate, zoom: 15, bearing: 0, viewingAngle: 0)

}

COMPLETE MAPVIEWCONTROLLER CODE WITH GOOGLE MAP:

import UIKit

import GoogleMaps

class ViewController: UIViewController, CLLocationManagerDelegate {

@IBOutlet weak var mapView: GMSMapView!

private let locationManager = CLLocationManager()

override func viewDidLoad() {

super.viewDidLoad()

// Do any additional setup after loading the view.

locationManager.delegate = self

locationManager.requestWhenInUseAuthorization()

}

func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {

guard status == .authorizedWhenInUse else {

return

}

// 4

locationManager.startUpdatingLocation()

//5

mapView.isMyLocationEnabled = true

mapView.settings.myLocationButton = true

}

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

guard let location = locations.first else {

return

}

mapView.camera = GMSCameraPosition(target: location.coordinate, zoom: 15, bearing: 0, viewingAngle: 0)

}

}

Mohd Danish Khan

Written by

React-Native / iOS / Flutter Software Engineer, Machine Learning Researcher

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade