简体   繁体   中英

How to set up a map toggle button in android

I have a menu with a button that says 'Toggle Map'. When this is clicked, I want the map type to toggle between standard map and satellite view. I don't know the code to do this or what I would have to do to achieve this.

Please help me code it and help me understand where to put the code and why. I have the following which somebody helped me write to start this off, but really don't know the code or actions to take from here.

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.mapview :

default :

return super.onOptionsItemSelected(item);
}
}

Anybody?

So, your "somebody" help you with the menu button, which is a start. When the user will press menu, and choose the menu button "map", with the id "mapview", it will call your code in the switch. You just need to check if you're in the sattelite mode, and, if you are, stop it:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.mapview :
   if(mMapView.isSatellite()) {
      mMapView.setSatellite(false); 
   } else {
      mMapView.setSatellite(true); 
   }


default :

return super.onOptionsItemSelected(item);
}
}

But you need to get the MapView mMapView. For that, in the onCreate of your Activity, just use findViewById(R;id.mapviewid);

mapviewid is an example, but you will find it in the file you use in the onCreate(), where you do setContentView(R.layout.something). Go to this file, and search the MapView, to get the id.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM