FREE TRIAL: Accelerate your app development career with Hacking with Swift+! >>

How to read the user’s location using LocationButton

Paul Hudson    @twostraws   

Updated for Xcode 13.0

New in iOS 15

SwiftUI has a dedicated LocationButton view for displaying a standard UI for requesting user location. Sadly, it doesn’t do any of the work of getting the location for us, but it at least has a recognizable user interface that we can work with.

In order to use this, you first need to import two frameworks, one for reading the location and one for showing the button:

import CoreLocation
import CoreLocationUI

Next, you create some kind of ObservableObject that is able to request the user’s location on demand. This needs to create a CLLocationManager and call its requestLocation() method on demand. You can then put that inside a SwiftUI view showing a location button.

So, you might write something like this:

class LocationManager: NSObject, ObservableObject, CLLocationManagerDelegate {
    let manager = CLLocationManager()

    @Published var location: CLLocationCoordinate2D?

    override init() {
        super.init()
        manager.delegate = self
    }

    func requestLocation() {
        manager.requestLocation()
    }

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        location = locations.first?.coordinate
    }
}

struct ContentView: View {
    @StateObject var locationManager = LocationManager()

    var body: some View {
        VStack {
            if let location = locationManager.location {
                Text("Your location: \(location.latitude), \(location.longitude)")
            }

            LocationButton {
                locationManager.requestLocation()
            }
            .frame(height: 44)
            .padding()
        }
    }
}

A rectangular blue button with a location arrow and the words “Current Location”.

Apple provides a handful of variant designs for the button, activated by passing one of them in with its initializer – try LocationButton(.shareMyCurrentLocation), for example.

A rectangular blue button with a location arrow and the words “Share My Current Location”.

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for iOS devs who want to become complete senior developers — from October 18th to 24th. Learn how to apply iOS app architecture patterns through a series of lectures and practical coding sessions.

Learn more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Similar solutions…

BUY OUR BOOKS
Buy Pro Swift Buy Swift Design Patterns Buy Testing Swift Buy Hacking with iOS Buy Swift Coding Challenges Buy Swift on Sundays Volume One Buy Server-Side Swift (Vapor Edition) Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Advanced iOS Volume Three Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with macOS Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Server-Side Swift (Kitura Edition) Buy Beyond Code

Was this page useful? Let us know!

Average rating: 5.0/5

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.