Today I’m going to show you guys how easily one can integrate Google Maps to their apps.
Prerequisites:
- Xcode 6.3 or above
- CocoaPods Installed
Create an empty project, then open your terminal and type the following lines:
1 |
cd ~/Path/To/Folder/Containing/Your Newly Created Project |
Just type cd and drag your project to the terminal it will automatically detect your path to the project.
Now type:
1 |
pod init |
Then type the following lines it will open your Podfile:
1 |
open -a Xcode Podfile |
Copy the following lines in your Podfile after target:
1 2 |
source 'https://github.com/CocoaPods/Specs.git' pod 'GoogleMaps' |
Your Podfile will look something like this:
Save it and close it then come back to your terminal and type:
1 |
pod install |
It will start installing the SDK.
And you’ll see outputs like:
1 2 3 4 5 |
Analyzing dependencies Downloading dependencies Installing GoogleMaps Generating Pods project Integrating client project |
Now go back to your project and you’ll see a file named yourproject.xcworkspace, similar to this:
Open its xcworkspace file, add a bridging header to your project and import GoogleMaps:
[code language=”css”]#import <GoogleMaps/GoogleMaps.h>[/code]
Then open your AppDelegate.swift and add your iOS API Key that you obtained from Google Developers’console:
[code language=”css”] GMSServices.provideAPIKey(“Your-iOS-API-Key”)[/code]
Now open your Main.storyboard and drag a UIView:
and apply constraints to it:
Click on your View and add GMSMapView as its Custom Class:
Now create a layout of the View of type GMSMapView:
Its time to the set the camera of the map. Lets say we want it to appear over Sydney, Australia when our app starts, then in your viewDidLoad do:
1 2 |
let camera: GMSCameraPosition = GMSCameraPosition.cameraWithLatitude(-33.8650, longitude: 151.2094, zoom: 6.0) customView.camera = camera |
Now set up the marker; for that we are going to use GoogleMaps’ GMSMarker class. In your viewDidLoad write:
1 2 3 4 5 6 |
var marker = GMSMarker() marker.position = CLLocationCoordinate2DMake(-33.8650, 151.2094) marker.title = "Sydney" marker.snippet = "Australia" marker.appearAnimation = kGMSMarkerAnimationNone marker.map = customView |
Save and Run it, this is the output that you’ll get:
I have uploaded this project on Github, so you guys can check it out.
If you guys have any question, please leave a comment.