How can I change map type using checkboxes

  android, google-maps, java

I am working on a project in Android Studio that has some classes and a google maps activity. I want to change the map type using checkboxes. The checkboxes are in another class, different from the google maps class. I tried this:

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.google.android.gms.maps.GoogleMap;

import static com.example.googlemaps.MapActivity.mMap;

public class MapSettings extends AppCompatActivity implements View.OnClickListener{

    CheckBox mHybrid, mTerrain, mNormal, mSatellite;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map_settings);

        mHybrid = (CheckBox) findViewById(R.id.hybrid);
        mHybrid.setOnClickListener(this);
        mTerrain = (CheckBox) findViewById(R.id.terrain);
        mTerrain.setOnClickListener(this);
        mNormal = (CheckBox) findViewById(R.id.normal);
        mNormal.setOnClickListener(this);
        mSatellite = (CheckBox) findViewById(R.id.satellite);
        mSatellite.setOnClickListener(this);
        mNormal.setChecked(true);

    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.hybrid:
                if(mHybrid.isChecked()) {
                    mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
                    mHybrid.setChecked(true);
                    mNormal.setChecked(false);
                    mTerrain.setChecked(false);
                    mSatellite.setChecked(false);
                }
                break;

            case R.id.terrain:
                if(mTerrain.isChecked()) {
                    mMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
                    mTerrain.setChecked(true);
                    mNormal.setChecked(false);
                    mHybrid.setChecked(false);
                    mSatellite.setChecked(false);
                }
                break;

            case R.id.normal:
                if(mNormal.isChecked()) {
                    mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
                    mNormal.setChecked(true);
                    mTerrain.setChecked(false);
                    mHybrid.setChecked(false);
                    mSatellite.setChecked(false);
                }
                break;

            case R.id.satellite:
                if(mSatellite.isChecked()) {
                    mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
                    mSatellite.setChecked(true);
                    mNormal.setChecked(false);
                    mTerrain.setChecked(false);
                    mHybrid.setChecked(false);
                }
                break;
        }
    }
}

After I am checking a box to test it, it doesn’t change at all and when I come back to MapSettings the "Normal" checkbox is checked

Source: Android Questions

LEAVE A COMMENT