Creating an Android Map Menu to Change Map Type

I have a map in an Android application. By default, it displays the satellite, but I turned it off to display only roadmaps. However, I wonder how I would build the menu, so when the user clicked the menu button, he would display the section at the bottom with β€œswitching satellite map”. (In the future I will add other items to the menu)

Thanks to everyone who can help with this.

+4
source share
3 answers

This is my implementation that works great with the GoogleMaps v2 API. It shows a dialog with four switches from which you can choose the type of map. The selected card type is also selected.

Android AlertDialog to select GoogleMaps MapType

This code preferably goes into your activity containing the card. Just make sure your map is up and running correctly before calling showMapTypeSelectorDialog (). I also recommend using resource strings for labels.

private GoogleMap mMap; ... private static final CharSequence[] MAP_TYPE_ITEMS = {"Road Map", "Hybrid", "Satellite", "Terrain"}; private void showMapTypeSelectorDialog() { // Prepare the dialog by setting up a Builder. final String fDialogTitle = "Select Map Type"; AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle(fDialogTitle); // Find the current map type to pre-check the item representing the current state. int checkItem = mMap.getMapType() - 1; // Add an OnClickListener to the dialog, so that the selection will be handled. builder.setSingleChoiceItems( MAP_TYPE_ITEMS, checkItem, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int item) { // Locally create a finalised object. // Perform an action depending on which item was selected. switch (item) { case 1: mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE); break; case 2: mMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN); break; case 3: mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID); break; default: mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL); } dialog.dismiss(); } } ); // Build the dialog and show it. AlertDialog fMapTypeDialog = builder.create(); fMapTypeDialog.setCanceledOnTouchOutside(true); fMapTypeDialog.show(); } 
+18
source

Just add this to your activity:

  @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.menu_items, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.item1 : //do what you like default : return super.onOptionsItemSelected(item); } } 

This should be in a separate XML file (possibly / res / menu / menu_items.xml)

 <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/item1" android:icon="@android:drawable/ic_menu_help" android:title="Help" /> <item android:id="@+id/item2" android:icon="@android:drawable/ic_menu_manage" android:title="Settings" /> </menu> 
0
source

Build your menu / button / tab / your choice and in the event listener execute: mapView.setSatellite (false); this will convert the satellite to a roadmap. Greetings.

0
source

All Articles