MapView not returning to normal state after clicking toggle switch

后端 未结 2 888
既然无缘
既然无缘 2021-01-24 10:08

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 occu

相关标签:
2条回答
  • 2021-01-24 10:38

    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);
                }
    
    0 讨论(0)
  • 2021-01-24 10:41

    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.

    0 讨论(0)
提交回复
热议问题