MapView not returning to normal state after clicking toggle switch
Asked Answered
B

2

1

I'm trying to get my map view to return to the normal style when I flick the switch within my fragment to the the OFF position but it's not working. The following is what occurs during the process that I am facing:

  1. Fragment (containg map) and switch appeared
  2. Flicked switch to ON position
  3. Styled map appeared
  4. Flicked switch to OFF position
  5. Map doesn't change

I tried using mGoogleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL); but the map still remains stuck on the styled map view when I flick the switch. Not really sure as to what has gone wrong. What must be done to resolve this problem?

public class FragmentMap extends android.support.v4.app.Fragment implements OnMapReadyCallback {

    private SwitchCompat swt;

    public FragmentMap() {
    }

    GoogleMap mGoogleMap;
    MapView mMapView;

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_map, container, false);

        mMapView = (MapView) v.findViewById(R.id.map_park);
        mMapView.onCreate(savedInstanceState);
        mMapView.getMapAsync(this);

        swt = (SwitchCompat) v.findViewById(R.id.switch_map_park);
        swt.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    boolean success = mGoogleMap.setMapStyle(new MapStyleOptions(getResources()
                            .getString(R.string.style_json)));

                    if (!success) {
                        Log.e("TabFragmentMap", "Style parsing failed.");
                    }
                }else{
                    // What goes here?
                    mGoogleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
                }
            }
        });

        return v;
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mGoogleMap = googleMap;
        mGoogleMap.getUiSettings().setZoomControlsEnabled(true);
        mGoogleMap.setBuildingsEnabled(true);
        mGoogleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
    }

    @Override
    public void onResume() {
        super.onResume();
        mMapView.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        mMapView.onPause();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mMapView.onDestroy();
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mMapView.onSaveInstanceState(outState);
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        mMapView.onLowMemory();
    }
}
Barger answered 1/10, 2016 at 21:11 Comment(0)
H
2

In your else code, you are using setMapType while you should be using setMapStyle with a null parameter to clear the previously set styling, as mentioned here.

Set to null to clear any previous custom styling.

So, your code should go like this:

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if(isChecked){
                boolean success = mGoogleMap.setMapStyle(new MapStyleOptions(getResources()
                        .getString(R.string.style_json)));

                if (!success) {
                    Log.e("TabFragmentMap", "Style parsing failed.");
                }
            }else{
                boolean success = mGoogleMap.setMapStyle(null);

                if (!success) {
                    Log.e("TabFragmentMap", "Removing style failed.");
                }
            }
        }

You should also return the Switch back to its previous checked state in both your if (!success) conditions and show a Toast to the users so they can understand what is happening.

Heimer answered 3/11, 2016 at 13:23 Comment(1)
Unfortunately, I've never worked with Wearables before.Heimer
N
-1

The else of this part:

            if(isChecked){
                boolean success = mGoogleMap.setMapStyle(new MapStyleOptions(getResources()
                        .getString(R.string.style_json)));

                if (!success) {
                    Log.e("TabFragmentMap", "Style parsing failed.");
                }
            }else{
                // What goes here?
                // mGoogleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
            }

You need to uncomment it

            else {
                // What goes here?
                mGoogleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
            }
Nuncupative answered 1/10, 2016 at 21:37 Comment(2)
That was uncommented in the first place but it STILL doesn't work.Barger
Well then look at the other thing: boolean success = mGoogleMap.setMapStyle(new MapStyleOptions(getResources() .getString(R.string.style_json))); is for activated, the opposite of that needs to go no in the else statementNuncupative

© 2022 - 2024 — McMap. All rights reserved.