When using Google Maps on the bottom left you have an icon to change the map type, I want to add this functionnality to my small Android application but I can't figure out how.
Here is what I have:
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
getLastLocation();
mMap.setMyLocationEnabled(true);
mMap.getUiSettings().setMyLocationButtonEnabled(true);
mMap.getUiSettings().setMapToolbarEnabled(true);
mMap.getUiSettings().setCompassEnabled(true);
mMap.getUiSettings().setZoomControlsEnabled(true);
// different types of maps
/* mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
mMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);*/
}
I tried to check the getUiSettings but it seems like there isn't an option for that ? (which I find a bit strange)
Change map view in Google Maps – MobileOpen Google Maps. Tap the layers button at the top right. From the menu that opens, select a view: Default, Satellite, or Terrain. From the Map Details section enable any one of the detail options.
It's simple :)
Use FAB in your map XML File like this:
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|right"
android:layout_margin="@dimen/fab_margin"
app:backgroundTint="#0D640B"
app:elevation="20dp"
app:fabSize="mini"
app:rippleColor="#0D640B"
app:srcCompat="@android:drawable/ic_menu_map_type" />
Then add this lines in your Map Activity:
FloatingActionButton fab = root.findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (GoogleMap.MAP_TYPE_NORMAL == mMap.getMapType()) {
mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
} else {
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With