package eu.ztsh.garmin.mapbox import com.mapbox.common.location.Location import com.mapbox.navigation.core.trip.session.LocationMatcherResult import com.mapbox.navigation.core.trip.session.LocationObserver import com.mapbox.navigation.ui.maps.camera.transition.NavigationCameraTransitionOptions import eu.ztsh.garmin.Garmin class LocationObserver(private val mapControl: MapControl) : LocationObserver { /** * Gets notified with location updates. * * Exposes raw updates coming directly from the location services * and the updates enhanced by the Navigation SDK (cleaned up and matched to the road). */ private var firstLocationUpdateReceived = false override fun onNewRawLocation(rawLocation: Location) { // not handled } override fun onNewLocationMatcherResult(locationMatcherResult: LocationMatcherResult) { val enhancedLocation = locationMatcherResult.enhancedLocation // update location puck's position on the map mapControl.navigationLocationProvider.changePosition( location = enhancedLocation, keyPoints = locationMatcherResult.keyPoints, ) // update camera position to account for new location mapControl.viewportDataSource.onLocationChanged(enhancedLocation) mapControl.viewportDataSource.evaluate() Garmin.instance.process(locationMatcherResult) // if this is the first location update the activity has received, // it's best to immediately move the camera to the current user location if (!firstLocationUpdateReceived) { firstLocationUpdateReceived = true mapControl.navigationCamera.requestNavigationCameraToOverview( stateTransitionOptions = NavigationCameraTransitionOptions.Builder() .maxDuration(0) // instant transition .build() ) } } }