Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to display the small button to change the map type using Android and Maps fragment?

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)

like image 804
J.erome Avatar asked Apr 29 '20 08:04

J.erome


People also ask

How do I change the map on my screen?

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.


1 Answers

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);
            }
like image 166
Mrunal Avatar answered Sep 28 '22 03:09

Mrunal