How can I get user location with MapKit?

I write map app for iOS 17 with Xcode 15. And I have problem with detecting user location.

So, I add the parameters to info

info

And I create Map() with MapUserLocationButton()

var body: some View {
        Map(scope: mapScope){
            UserAnnotation()
        }
        .mapControls() {
            VStack {
                MapUserLocationButton(scope: mapScope)
                MapScaleView(scope: mapScope)
            }
        }
        .mapControlVisibility(Visibility.visible)
    }

And if I push MapUserLocationButton I got error:
CLLocationManager(<CLLocationManager: 0x28377a500>) for <MKCoreLocationProvider: 0x280773210> did fail with error: Error Domain=kCLErrorDomain Code=1 "(null)"

I also tried to add:

 let locationManager = CLLocationManager()

.onAppear {
    locationManagerDidChangeAuthorization(locationManager)
}

func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {
        switch manager.authorizationStatus {
        case .authorizedWhenInUse:  // Location services are available.
            enableLocationFeatures()
            break
            
        case .restricted, .denied:  // Location services currently unavailable.
            disableLocationFeatures()
            break
            
        case .notDetermined:        // Authorization not determined yet.
            manager.requestWhenInUseAuthorization()
            break
            
        default:
            break
        }
    }

but it didn’t get result.

What I do wrong?


Console screenshot:
Console screenshot

  • Did you add the proper entitlements and the usage key in Info.plist?

    – 

  • @vadian Yes. I added a picture to the quest

    – 




  • 1

    have you checked this? stackoverflow.com/a/17164298/10284355

    – 

  • @KrunalNagvadia, I set Allow Location Simulation and Debug > Location, but it didn’t fix a problem. I can also reproduce this error on real device

    – 

  • 1

    Can you please attach a full screenshot of the console so I can check the error in detail?

    – 

Leave a Comment