Want to display Location in Application ? Know how to amalgamate Google Map with Swift

04 Mar 2016 Author : Nirali Shah
Want to display Location in Application ? Know how to amalgamate Google Map with Swift

Nowadays controversies for app development are highly faced off in top app  Development companies. Developers are constantly striving to develop more and more better apps. Users demand more and more features in stipulated budget while developers are in race to provide unique features helping them to stand like a rock in market. There are some functionalities like chatting , Image sharing , Location which are like primary needs to be figured out for any layman apps.

 

Amongst these Chatting , Camera functionalities are the most common to be found in any of the app but there may exist a scenario where you need to send your location on other end but your iOS app does not have that trait included in your app then what would be the case?

 

Don't Worry Hyperlink Infosystem have made a way out . We will help you to get deep insights about how to integrate Google Maps in your Swift Platform.

 

But firstly let's have an glimpse about what is Google Map SDK all about and how it will help us in our app

 

Introduction about Google Map SDK

 

Google Maps SDK for iOS permits us to integrate maps to our iOS applications. Google Maps SDK naturally deals the Google Maps, and also acts on the user’s activities such as

 

 

Tap

Zooming

Movement

 

 

It allows you to fetch Users ongoing Location . One can also get the map location by its latitude and longitude of the place.

 

Now, to integrate Google Maps in your iOS Application you need to have and API key and for the same you need Google Account.

 

How to get API Key ?


For Accessing Google Map SDK we need API Key . API Key is nothing but an key using which we can make out calls to Google API from iOs Application.

 

Follow these simple steps to get API Key

 

Step 1: Sign in to Developers Account and create a project into that and name the project .Then click -> Create button.

 

 

 

 

Step 2: Then the next step is selection of project by clicking -> Select Project button.

 

 

 

 

Step 3: Click -> Enable and Manage API

 

 

Step 4: Now it will list out all the available Google API . Select the Google MAP SDK for iOS mentioned under Google Maps API

 

 

 

Step 5: Now it will prompt you with window to Enable API . Now click -> Enable API

 

 

 

 

Step 6: Now you have successfully enabled Google Map SDK and to access it you need to add credentials into that .Click -> Go to Credentials to create it.

 

 

Step 7: Click -> New Credentials

 

 

Step 8: A drop down box will be displayed when clicked on New Credentials . Select ->API Key


 

 

Step 9: Now click -> iOS Key

 

 

 

Step 10 : Now it will ask you to name out the Api Key and also addition for bundle identifier .

One can add multiple identifiers that will accept request from the iOS application from any of the bundles listed over there.

When you are done with all the configurations just click on “Create” button.

 

 

 

 

Step 11: Now finally your Api is created and is being displayed on screen .

 

You will need this Api to access Google Map SDK in your app.

 

 

 

 

Now your API key is ready . Let's configure the project and integrate Google Map into that


 

 

How to Integrate Google Map into Swift ?


 

Step1 : Firstly create a new X code Project.

 

 

Step 2: To integrate Google Maps in iOS App you need GoogleMaps.framework

 

 

Step 3: Now drag and drop GoogleMaps.Framework into the project in X code.

 

 

 

Step 4: Now a new tab will appear that will opt you to choice option for “ adding files”. Now verify on the Copy Items on needed check box .

 

If its not checked then make it checked true and then click finish Button.

 

 

 

Step 5: Now you can watch out GoogleMap.Framework in your Project.

 

Right click -> Select Show in finder then,

 

Double click -> GoogleMaps.Framework -> Select Resources

 

Now you can see googleMap.bundle . Drag it into the project.

 

 

 

Step 6: Now a new tab will appear that will opt you to choose option for “ adding files”. Now check on the Copy Items if needed check box . If its checked then make it unchecked true and then click finish Button.

 

 

Step 7: Google Maps Framework also needs other frameworks such as :

 

Accelerate.framework

AVFoundation.framework

CoreBluetooth.framework

CoreData.framework

CoreGraphics.framework

CoreLocation.framework

CoreText.framework

GLKit.framework

ImageIO.framework

libc++.dylib

libicucore.dylib

libz.dylib

OpenGLES.framework

QuartzCore.framework

Security.framework

SystemConfiguration.framework

 

Now to include these frameworks in the project select your project and click -> Build Phases . Exploring the link “binary With Libraries” and click on + button.

 

 

Choose and add all the above listed framework

 

 

 

Step 8 : Adding Bridge

 

As GoogleMaps.Framework is available in objective C language a bridging header needs to be built to add objective C code to Swift Platform.


To add Bridging Header follow these steps :

 

1) create New file and add temporary file.

2) Select Objective C and click Next.

3) Add filename and click Next and then click on Create.

4) Now click-> Create Bridging Header and add following lines:

#import

Delete the temporary file created.

 

Step 9: Displaying Map in Application

 

1) Open AppDelegate.Swift and paste following lines in method under:

application(_didFinishLaunchingWithOptions:)

 

Code:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

GMSServices.provideAPIKey("")

return true

}

Here you will use your API key that you have generated to integrate Google Map in Swift.

 

2) Now in ViewController.Swift add below code lines under method viewDidLoad() method


Code:

override func viewDidLoad()

{

let camera: GMSCameraPosition = GMSCameraPosition.cameraWithLatitude(22.300000, longitude: 70.783300, zoom: 10.0)

vwGMap = GMSMapView.mapWithFrame(self.view.frame, camera: camera)

vwGMap.camera = camera

}

 

 

Step 10 : Accessing Users Recent Location

 

1) Now Add your class to CLLocationManagerDelegate and create variables for the CLLocationManager and GMSMapView as below:

 

Var locationManager = CLLocationManager();

Var vgms = GMSMapView();

 

2) Now add following code in Controller

 

override func viewDidLoad()

{

super.viewDidLoad()locationManager.delegate = self

locationManager.desiredAccuracy = kCLLocationAccuracyKilometer

// A minimum distance a device must move before update event generated

locationManager.distanceFilter = 500

// Request permission to use location service

locationManager.requestWhenInUseAuthorization()

// Request permission to use location service when the app is run

locationManager.requestAlwaysAuthorization()

// Start the update of user's location

locationManager.startUpdatingLocation()

 

// Add GMSMapView to current view

self.view = vwGMap

}

 

Step 11:

 

1) If we want to request permission by locationManager.requestWhenInUseAuthorization(), it will call locationManager(_didChangeAuthorizationStatus:) where location will be enabled and can access user current location.

Code:

func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus)

{

if (status == CLAuthorizationStatus.AuthorizedWhenInUse)

{

vwGMap.myLocationEnabled = true

}

}

 

 

2) If we want to get the updated location of the user then locationManager(_didUpdateLocations) method is called at regular intervals.

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

 

{

let newLocation = locations.last

vwGMap.camera = GMSCameraPosition.cameraWithTarget(newLocation!.coordinate, zoom: 15.0)

vwGMap.settings.myLocationButton = true

self.view = self.vwGMap

}

 

 

Step 12: Creating Location Marker and setting location

To create marker at users location make GSMMarker variable

 

glet marker = GMSMarker()

If you want to have marker at specified location then use below lines

 

marker.position= CLLocationCoordinate2DMake(newLocation!.coordinate.latitude, newLocation!.coordinate.longitude)

marker.map = self.vwGMap

 

Now you will be able to watch out marker at user's current location.

 

I hope now you are aware with how to integrate your Google Map with Swift platform. Got to know about i phone application development . If you are not aware then no need to worry contact us and get in touch with excellent app developers . Stay tuned to us ..!

Get Free Quote Now !