Wednesday, 20 November 2019

How can you draw a straight line between 2 Points on a Mapbox map?

I have loaded a Mapbox map in my Fragment:

xml

<com.mapbox.mapboxsdk.maps.MapView
    android:id="@+id/mapView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    mapbox:mapbox_cameraZoomMax="@integer/maxZoom"
    mapbox:mapbox_cameraZoom="@integer/defaultZoom"
    mapbox:mapbox_cameraZoomMin="@integer/minZoom"
    mapbox:mapbox_uiRotateGestures="false"
    mapbox:mapbox_uiTiltGestures="false"
    mapbox:mapbox_uiScrollGestures="false"
    mapbox:mapbox_uiDoubleTapGestures="false" />

Fragment

    /** Initialise Mapbox **/
        mapView = view.findViewById(R.id.mapView)
        mapView?.onCreate(savedInstanceState)
        val destinationMarker = ContextCompat.getDrawable(activity, R.drawable.dest_logo) ?: return
        mapView?.getMapAsync { mapboxMap ->
            this.mapboxMap = mapboxMap
            this.mapboxMap.setStyle(Style.MAPBOX_STREETS) { style ->
                style.addImage("destination", destinationMarker)
                showUserLocation(style)
                resetCamera()
            }
        }

@SuppressWarnings("MissingPermission")
private fun showUserLocation(style: Style){
    // If permissions are granted, show/get user user1location. Else, return to TurnOnLocationActivity
    if (PermissionsManager.areLocationPermissionsGranted(activity)){
        val activity = activity ?: return
        val locationComponentOptions = LocationComponentOptions.builder(activity)
            .bearingTintColor(Color.WHITE)
            .accuracyAlpha(0.1f)
            .build()

        val locationComponentActivationOptions = LocationComponentActivationOptions
            .builder(activity, style)
            .locationComponentOptions(locationComponentOptions)
            .useDefaultLocationEngine(true)
            .build()
        val mapView = mapView ?: return returnToLoginPage()
        if (!style.isFullyLoaded) return returnToLoginPage()
        symbolManager = SymbolManager(mapView, mapboxMap, style)
        locationComponent = mapboxMap.locationComponent
        locationComponent?.activateLocationComponent(locationComponentActivationOptions)
        locationComponent?.isLocationComponentEnabled = true
        locationComponent?.cameraMode = CameraMode.TRACKING
        locationComponent?.renderMode = RenderMode.COMPASS
        return createLocationEngine()
    } else {
        Toast.makeText(context, "Permissions not granted", Toast.LENGTH_LONG).show()
        return returnToLocationPage()
    }

}

@SuppressWarnings("MissingPermission")
private fun createLocationEngine(){
    // Get current user1location
    val activity = activity ?: return
    locationEngine = LocationEngineProvider.getBestLocationEngine(activity)
    // After user1location has been loaded, configure mapBox settings
    mapboxMap.uiSettings.isCompassEnabled = false
}

I would like to draw a straight line between 2 Points on my map. Say I have -37.791890, 145.119387 and -37.790597, 145.116213 as my 2 Points - how would I draw a straight line?



from How can you draw a straight line between 2 Points on a Mapbox map?

No comments:

Post a Comment