Skip to content

Go to user location control #145

@derekchai

Description

@derekchai

I've been playing around with this package and I was wondering if there is a way to add a button which sets the camera to the user's location. My first thought would have been:

struct MainMapView: View {
    @State private var camera = MapViewCamera.trackUserLocationWithHeading(zoom: 10)

    var body: some View {
        MapView(
            styleURL: styleURL,
            camera: $camera,
            locationManager: locationManager
        )
        .mapUserAnnotationStyle(
            .init(
                approximateHaloBorderColor: .blue,
                approximateHaloBorderWidth: 50,
                approximateHaloFillColor: .blue,
                approximateHaloOpacity: 0.5,
                haloFillColor: .blue,
                puckArrowFillColor: .blue,
                puckFillColor: .blue,
                puckShadowColor: .white,
                puckShadowOpacity: 0.5
            )
        )
        .overlay(alignment: .leading, content: {
            Button("Go to location") {
                camera = MapViewCamera.trackUserLocationWithHeading(zoom: 10)
            }
        })
        .onAppear {
            locationService.requestWhenInUseAuthorization()
            locationService.startUpdatingLocation()
            locationService.startUpdatingHeading()
        }
    }
}

... but this only seems to work some of the time: specifically, only after the camera has updated (from a pan, zoom, etc.); it will not update during a pan.

Just wondering if there is an intended way to implement this kind of behaviour?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions