Making an android map menu to change map type

前端 未结 3 1258
野性不改
野性不改 2020-12-28 11:58

I have a map in my android app. By default it shows the satellite view, but I have turned it off to only show the road maps view. However, I am wondering how I would constru

相关标签:
3条回答
  • 2020-12-28 12:20

    Construct your menu/button/tab/your-choice and in the event listener do: mapView.setSatellite (false); this will transform the satellite into road map. Cheers.

    0 讨论(0)
  • 2020-12-28 12:27

    This is my implementation that works just fine with GoogleMaps API v2. It shows a dialog with four radio buttons from which you can select a map type. The currently selected map type is also already selected.

    Android AlertDialog to select GoogleMaps MapType

    This code preferably goes into your activity that holds the map. Just make sure your map is initiated and displays correctly before you call showMapTypeSelectorDialog(). I also recommend using Resource Strings for the 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();
    }
    
    0 讨论(0)
  • 2020-12-28 12:35

    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 (maybe /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 讨论(0)
提交回复
热议问题