Do you want to learn implementing Google Maps in our iOS Application. So here is Google Maps iOS Tutorial. Today we will learn how to add Google Maps in our iOS Application using Swift. So lets start.
Contents
Google Maps iOS Tutorial Video
- Here is the video tutorial about implementing Google Maps to iOS App, you can go with it as well.
Creating a new Xcode Project
- Now the first thing is creating a new Xcode Project. So I am creating GoogleMapsExample.
- Now once the project is loaded we will first add Google Maps SDK using CocoaPods.
Adding Google Maps using CocoaPods
- Right click on your project and select New File… as shown in the image.
- Now create an Empty file named Podfile.
- Inside the Podfile write the following code.
source 'https://github.com/CocoaPods/Specs.git' target 'GoogleMapsExample' do pod 'GoogleMaps' pod 'GooglePlaces' end
- In the above code GoogleMapsExample is my project name, so make sure it matches with your project name.
- Now navigate to your project directory in terminal and run the command pod install.
- It will download and install the Google Map Dependencies.
- Now close the Xcode Project and open yourproject.xcworkspace. For the detail steps about using CocoaPods you can check the last tutorial about Google Signin in Swift.
- Now we need an API Key.
Getting an API Key
- Go to this link and click on GET A KEY.
- Now it will ask you to select a project. So select a project and Enable the API.
- After clicking Enable API you will get you API Key.
- Now copy the API from here.
Implementing Google Maps in Xcode Project
- Now come to the Xcode project and open AppDelegate.swift. Here you need to provide the API key as I provided in the following code snippet.
// // AppDelegate.swift // GoogleMapsExample // // Created by Belal Khan on 03/06/17. // Copyright © 2017 Belal Khan. All rights reserved. // import UIKit //you should import google maps import GoogleMaps @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { //this line should be added GMSServices.provideAPIKey("AIzaSyCJV5rHYEfQISiDi1qD_AIFg338KDFlCJM") // Override point for customization after application launch. return true }
- Now come to ViewController.swift.
// // ViewController.swift // GoogleMapsExample // // Created by Belal Khan on 03/06/17. // Copyright © 2017 Belal Khan. All rights reserved. // import UIKit import GoogleMaps class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() //creating a camera let camera = GMSCameraPosition.camera(withLatitude: 23.431351, longitude: 85.325879, zoom: 6.0) //this is our map view let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera) //adding mapview to view view = mapView //creating a marker on the map let marker = GMSMarker() marker.position = CLLocationCoordinate2D(latitude: 23.431351, longitude: 85.325879) marker.title = "Ranchi, Jharkhand" marker.map = mapView } }
- Now run the application in Simulator.
- Bingo! It is working absolutely fine.
So thats all for this Google Maps iOS Tutorial friends. If you are having any queries or confusions regarding this Google Maps iOS Tutorial then lets have a discussion on the comment section. And don’t forget to SHARE this post if you found it helpful. Thank You 🙂
nilesh sinha says
Thank you Belal for Wonderful tutorial
Daniel says
Hello,
I want to us, is it possible to measure a distance using google API in iOS?
Thank You