Categories: Swift Programming

Google Maps with Custom View in iOS

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:

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:

pod init

Then type the following lines it will open your Podfile:

 open -a Xcode Podfile

Copy the following lines in your Podfile after target:

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:

pod install

It will start installing the SDK.
And you’ll see outputs like:

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:

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:

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.

Aaqib Hussain

Recent Posts

Things to know when moving to Germany

This article covers some important things you must know when you are considering a move…

3 years ago

Unit Testing in Android for Dummies

What is Unit Testing? In its simplest term, unit testing is testing a small piece…

4 years ago

Factory Design Pattern

In this article, you will learn about a type of Creational Design Pattern which is…

5 years ago

Creating Target specific Theme in iOS

In this tutorial, you will go through the use of targets to achieve two separate…

5 years ago

Facade Design Pattern

In this article, you will learn about a type of Structural Design Pattern which is…

5 years ago

Singleton Design Pattern

In this article you will learn about a type of Creational Design Pattern which is…

5 years ago