androidandroid-jetpack-composeandroid-maps

How to correctly use GoogleMap `locationSource` property with Compose?


I am trying to add GoogleMap to my app with Compose.

What is the point of adding GoogleMap locationSource property ?

With the code below :

    val locationSource = MyLocationSourceImpl (context) {
        // This is the LocationSource.OnLocationChangedListener I'd like to be executed.
    }

    GoogleMap(
            modifier = Modifier.fillMaxSize(),
            locationSource = locationSource,
    ) {
        [...]
    }

MyLocationSourceImpl activate function is not called automatically by GoogleMap, and anyways I find no option to tell GoogleMap which OnLocationChangedListener I'd like to activate.

I end up doing this :

    val locationSource = MyLocationSourceImpl(context).apply {
        activate {
            // This is my LocationSource.OnLocationChangedListener
        }
    }

    GoogleMap(
            modifier = Modifier.fillMaxSize(),
            locationSource = locationSource,  // what's the point of doing this ? It works well without it.
    ) {
        [...]
    }

But I don't understand why I should fill locationSource of GoogleMap as the activation/deactivation are handled outside of the component.

Any idea ?


Solution

  • GoogleMap's LocationSource is used to replace the default (in-built) location provider for the "my-location" map layer (the blue dot on the map) - see isMyLocationEnabled parameter of MapProperties. If you don't use this layer (having isMyLocationEnabled set to false) or if you are fine with the default location source, you don't need to set it at all. Also the activate function is called by the map if the "my location" feature is enabled only.