Skip to main content

How to read the user's location using LocationButton

About 2 minSwiftSwiftUIArticle(s)bloghackingwithswift.comcrashcourseswiftswiftuixcodeappstore

How to read the user's location using LocationButton 관련

SwiftUI by Example

Back to Home

How to read the user's location using LocationButton | SwiftUI by Example

How to read the user's location using LocationButton

Updated for Xcode 15

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”.
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”.
A rectangular blue button with a location arrow and the words “Share My Current Location”.
Similar solutions…
How to detect the location of a tap inside a view | SwiftUI by Example

How to detect the location of a tap inside a view
How to dynamically adjust the appearance of a view based on its size and location | SwiftUI by Example

How to dynamically adjust the appearance of a view based on its size and location
How to make a scroll view move to a location using ScrollViewReader | SwiftUI by Example

How to make a scroll view move to a location using ScrollViewReader
How to let users import videos using PhotosPicker | SwiftUI by Example

How to let users import videos using PhotosPicker
How to let users find and replace text | SwiftUI by Example

How to let users find and replace text

이찬희 (MarkiiimarK)
Never Stop Learning.