Search code examples
swiftuimapkitcllocationmanager

SwiftUI - Show predefined location as a user location on map


I am building an app that relies significantly on the user's location.

When the app starts, the user is presented with a series of views (on-boarding), and on the last view there are the following options:

A) Click to get your location:

in this case, we follow the standard procedure with plist and LocationManager to get the user's location and display it on the map.

B) Predefined location:

in this case, I will set a predefined location so that the app works as expected, but without the user's location.

Problem:

I managed to do both, but the problem I get is that in the case of B) Predefined location, I cannot see the user's dot on the map. I only see the annotationItem. In the case I get the user's location (option A), I can see both the user's location and the annotationItem on the map.

This is how I create the map:

Map(coordinateRegion: $buildingRegion, showsUserLocation: true, annotationItems: [building], annotationContent: { building in
    MapAnnotation(coordinate: building.coordinate) {
        AnnotationItem()
    }
})

Any idea why I can't show the predefined user location on the map? I feel like something is going on behind the scenes when the user chooses to allow the app to get the current location.

Thank you in advance.


Solution

  • The "blue dot" for the current user's location only appears on the map when the user allows location access.

    If the user has declined access, or you haven't asked, then no dot will be displayed.

    If you want to display a dot at a given location, then you will need to add a custom annotation.