iOS Tutorials: Show current location using MapView

OVERVIEW

This project will show you your current location on your devices. However, you won’t be able to get your current location if you run it on the simulator. You’ll be able to do it only through customized location using latitude and longitude which will be shown later on. If you would like to get your current location, please run it on your app and you’ll be able to get your current location.

In order to get the location, the device will first have to ask for user’s permission. You will be able to configure it here on the Info.plist

Then let’s get into designing the layout of the app. Here, we’ll be using MapView and a Button from the object library.

Here, you’ll have to create an IBOutlet for MapView and an IBAction for Button.

We will be creating our own variable that inherits the instance of CLLocationManager as we will be able to configure the location settings as shown below in viewDidLoad(). Also, we will be creating location that inherits CLLocation to retrieve the most recently user location.

import UIKit
import CoreLocation
import MapKit

class ViewController: UIViewController, CLLocationManagerDelegate {

    @IBOutlet weak var mapView: MKMapView!
    
    let locationManager = CLLocationManager()
    var location: CLLocation!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.requestWhenInUseAuthorization()
        locationManager.startUpdatingLocation()
        mapView.showsUserLocation = true

We’ll only be needing one function in this case which will updates the location all the time.

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
   self.location = locations.last as CLLocation!
}

With the IBAction, we are hoping that when a user clicked, it will show the current location of the user.

@IBAction func findMyLocation(_ sender: Any) {
    let center = CLLocationCoordinate2D(latitude: location!.coordinate.latitude, longitude: location!.coordinate.longitude)
    let span = MKCoordinateSpanMake(0.003, 0.003)
    let region = MKCoordinateRegion(center: center, span: span)
    self.mapView.setRegion(region, animated: true)
        
}

Your app will look like below if tested on a simulator. You may on the simulator pick a custom location which is what I did in my case. 

The code is uploaded on GitHub

  • Article By :
    Founder of DaddyCoding. Studied Computer Science, Information System and Information Technology at BYU-Hawaii. Currently spending most of my time researching and learning on helping to expose making iOS apps.

Random Posts

  • Unique Number

    OVERVIEW We are going to write an algorithm that will detect the String to make sure that it is unique in […]

  • Finding Prime Number

    OVERVIEW We will be looking for Prime Number by typing the number that we think is Prime Number. But what […]

  • Multiply by itself

    OVERVIEW We are going to write an algorithm that will multiply by itself. For example, if we take 10 * […]

  • iOS Tutorials: Animation with Lottie

    swift 4.2 | xcode 10 OVERVIEW Let’s be honest, animations on an app does make a huge difference. With that, […]

Leave a Reply

Your email address will not be published. Required fields are marked *

*
*