Skip to main content

My Location

To fetch and display your current location, ensure you have requested location permissions and then activate the LocationComponent.


Enable the location component​

fun enableLocationComponent(loadedMapStyle: Style) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
val locationComponent = map.locationComponent
locationComponent.activateLocationComponent(
LocationComponentActivationOptions.builder(this, loadedMapStyle).build()
)
locationComponent.isLocationComponentEnabled = true

// Center camera to my location
val location = locationComponent.lastKnownLocation
location?.let {
map.animateCamera(CameraUpdateFactory.newLatLngZoom(LatLng(it.latitude, it.longitude), 16.0))
}
} else {
ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.ACCESS_FINE_LOCATION), 1001)
}
}

See also: Android Map SDK overview.