Use Google Map Api to draw Marker and Path
To implement Google Map in iOS Project We have to Add Several components in our project and follow couple of steps.
- Get the latest version of Xcode
- Install the Google Map SDK
- You can install using both Cocoapods and Manually
The Google Maps SDK for iOS is available as a CocoaPods pod. CocoaPods is an open source dependency manager for Swift and Objective-C Cocoa projects. If you don't already have the CocoaPods tool, install it on macOS by running the following command from the terminal. For details, see the CocoaPods Getting Started guide. sudo gem install cocoapods Create a Podfile for the Google Maps SDK for iOS and use it to install the API and its dependencies:
- If you don't have an Xcode project yet, create one now and save it to your local machine. (If you're new to iOS development, create a Single View Application.)
- Create a file named Podfile in your project directory. This file defines your project's dependencies.
- Edit the Podfile and add your dependencies. Here is an example which includes the dependencies you need for the Google Maps SDK for iOS and Places API for iOS (optional):
source 'https://github.com/CocoaPods/Specs.git'
target 'YOUR_APPLICATION_TARGET_NAME_HERE
' do
pod 'GoogleMaps'
pod 'GooglePlaces'
end
Now follow this couple of steps
- Save the Podfile.
- Open a terminal and go to the directory containing the Podfile:
cd <path-to-project>
- Run the pod install command. This will install the APIs specified in the Podfile, along with any dependencies they may have.
- pod install
- Close Xcode, and then open (double-click) your
project's .xcworkspace
file to launch Xcode. From this time onwards, you must use the .xcworkspace file to open the project.
If you want to use google map in your iOS application the you must have to create an API
key from Google API Console.
- Go to the Google API Console.
- Create or select a project.
- Click Continue to enable the Google Maps SDK for iOS.
- On the Credentials page, get an API key.
- Note: If you have a key with iOS restrictions, you may use that key. You can use the same key with any of your iOS applications within the same project.
- From the dialog displaying the API key, select Restrict key to set an iOS restriction on the API key.
- In the Restrictions section, select iOS apps, then enter your app's bundle identifier. For example:
com.example.hellomap
. - Click Save.
- Your new iOS-restricted API key appears in the list of API keys for your project. An API key is a string of characters, something like this:
AIzaSyBdVl-cTICSwYKrZ95SuvNw7dbMuDt1KG0
- Add API key to your
AppDelegate.swift
file and import GoogleMaps - Add the following import statement:
import GoogleMaps
Add the following to your application(_:didFinishLaunchingWithOptions:)
method, replacing YOUR_API_KEY
with your API key:
GMSServices.provideAPIKey("YOUR_API_KEY")
If you are also using the Places API, add your key again as shown here:
GMSPlacesClient.provideAPIKey("YOUR_API_KEY")
- Open Your project
ViewController
and Drag a UIView and Go theIdentity Inspector
and set class toGMSMapView
and Create anOutlet
in yourViewController.swift
file
Now, add or update a few methods inside your app's default ViewController.swift to create and initialize an instance of GMSMapView
let camera = GMSCameraPosition.camera(withLatitude: 23.7740, longitude: 90.3664, zoom: 6.0) // point the initial location of map
mapView.camera = camera
mapView.delegate = self
mapView.isMyLocationEnabled = true
mapView.settings.myLocationButton = true
let marker = GMSMarker()
marker.position = CLLocationCoordinate2D(latitude: lat, longitude: long)
marker.title = “This is title”
marker.snippet = "GoogleMap"
marker.map = mapView