NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

How to read the user’s location using LocationButton

Paul Hudson    @twostraws   

Updated for Xcode 14.2

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 Waldo

SPONSORED Thorough mobile testing hasn’t been efficient testing. With Waldo Sessions, it can be! Test early, test often, test directly in your browser and share the replay with your team.

Try for free today!

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

Similar solutions…

BUY OUR BOOKS
Buy Pro Swift Buy Pro SwiftUI 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 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 Beyond Code

Was this page useful? Let us know!

Average rating: 3.3/5

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.