how to show current location on real device using Mapbox Kotlin

  android, kotlin, mapbox

on my emulator, I have been able to show fake location, but when I have tried my app on a real android device the location didn’t show, I don’t know what is wrong with my code i got this code from the Mapbox website and after following Mapbox documentation Show a user’s location here what I have come up with

class UserLocationActivity : AppCompatActivity(), PermissionsListener, OnMapReadyCallback {

    private var mapView: MapView? = null
    private var map: MapboxMap? = null

    private var permissionManager: PermissionsManager? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        Mapbox.getInstance(this, getString(R.string.access_token))

        setContentView(R.layout.activity_user_location)

        mapView = findViewById(R.id.usermapview)
        mapView?.onCreate(savedInstanceState)

        mapView?.getMapAsync { mapboxMap ->

            map = mapboxMap
            enableLocation(map?.style!!)
            mapboxMap.setStyle(Style.MAPBOX_STREETS) {

            }

        }

    }

    @SuppressLint("MissingPermission")
    fun enableLocation(style: Style){
        if (PermissionsManager.areLocationPermissionsGranted(this)) {
            val customLocationComponentOptions = LocationComponentOptions.builder(this)
                .trackingGesturesManagement(true)
                .accuracyColor(ContextCompat.getColor(this, R.color.mapbox_blue))
                .build()

            val locationComponentActivationOptions = LocationComponentActivationOptions.builder(this, style)
                .locationComponentOptions(customLocationComponentOptions)
                .build()


            map?.locationComponent?.apply {

                // Activate the LocationComponent with options
                activateLocationComponent(locationComponentActivationOptions)

                // Enable to make the LocationComponent visible
                isLocationComponentEnabled = true

                // Set the LocationComponent's camera mode
                cameraMode = CameraMode.TRACKING

                // Set the LocationComponent's render mode
                renderMode = RenderMode.COMPASS

            }

            } else {
            permissionManager = PermissionsManager(this)
            permissionManager?.requestLocationPermissions(this)
        }
    }

    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>,
                                            grantResults: IntArray) {
        permissionManager?.onRequestPermissionsResult(requestCode, permissions, grantResults)
    }

    override fun onExplanationNeeded(permissionsToExplain: MutableList<String>?) {
        Toast.makeText(this, "text", Toast.LENGTH_LONG).show()
    }

    @SuppressLint("MissingPermission")
    override fun onPermissionResult(granted: Boolean) {
        if (granted) {
            enableLocation(map?.style!!)
        } else {
            Toast.makeText(this, "text", Toast.LENGTH_LONG).show()
            finish()
        }
    }


    override fun onMapReady(mapboxMap: MapboxMap) {
        enableLocation(map?.style!!)
    }

}

Source: Android Questions

LEAVE A COMMENT