How to Get Current GPS Location in iOS App Using Core Location Framework

0
Shares
How to Get Current GPS Location in iOS App Using Core Location Framework
Rate this post

Get Current Location in iOS App Tutorial

Mobile applications use location data for multiple purposes, from social networking to driving navigations. They get the current GPS location using core location framework. This framework basically allows to get and monitor user’s device current location.

By providing location-based services, you can improve the user experience of your app. And you can add various location-based features that intrigue your app users.

Suppose you’re developing a travel app, now you can use user’s current GPS location and help them search for nearby hotels, resorts, restaurants, etc.. you can also find such location-based features in different photo apps that saves data where the pictures are taken.

In this iOS app tutorial, we’re going to build a simple demo app in which we’ll get the current location of a device.

Let’s Get Started

Create a new project under file menu and select “Single View Application” and click on next.

Map1

In the next tab, name your app project. Here, we’re naming it as SOMapLocation.

Map2

After creating a new project, add MapView in ViewController in Main.StoryBoard.

Map3

Map4

Next, set the outlet of button. We’ve set it as “Where am i”.

Now import MapKit in ViewController.swift

Once you import the MapKit in the project, Set “NSLocationAlwaysUsageDescription” with description in info.plist

Map6

create an object of CLLocationManager.

var locationManager: CLLocationManager = CLLocationManager()

Set Delegate of CLLocationManager in ViewController.swift

class ViewController: UIViewController, CLLocationManagerDelegate

Now for click event of “Where i am?” button we set delegate of CLLocationManager and go for authorize location service.

@IBAction func setCurrentLocation(sender: AnyObject) {

        if CLLocationManager.locationServicesEnabled() {

            locationManager.delegate = self

            locationManager.desiredAccuracy = kCLLocationAccuracyBest

            if self.locationManager.respondsToSelector(#selector(CLLocationManager.requestAlwaysAuthorization)) {

                locationManager.requestAlwaysAuthorization()

            } else {

                startUpdatingLocation()

            }

        }

    }

    func startUpdatingLocation() {

        locationManager.startUpdatingLocation()

    }

First time when you go for get your current location it will show a popup for allow to use current location for the app.

Map8

if you press “Allow” then CLLocationManager Delegate should call and you will get current location.

// MARK: - CLLocationManagerDelegate

    //Check authorization

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

        if status == .AuthorizedWhenInUse || status == .AuthorizedAlways {

            startUpdatingLocation()

        }

    }

    

    //Updated location

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

//Get last updated location(current)

        let location = locations.last! as CLLocation

        

        let center = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)

        let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.20, longitudeDelta: 0.20))

        

//Set region of current location in map view with zooming

        self.mpView.setRegion(region, animated: true)

//Show current location (blue dot) on map

        //self.mpView.showsUserLocation = true

    }

To show current location on map view you have to write single line code.

self.mpView.showsUserLocation = true

Map9

And we are done! Simple, right?

In case you face any problem in this demo, you can contact our developers for help.

However, Most of the successful startups like Zomato, Redbus, and Tinder provide services by integrating core location framework. So if you’ve such an app idea and require technical help to build the app, you can hire iPhone app developer from us.

Grab a free copy of core location framework demo from Github.

 
0
Shares
 

Wanna Create Location-based iOS App? Contact us Now.