开发者

Making an android map menu to change map type

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 construct a menu so when the user pressed the menu button, it would display a section at the bottom with 'toggle satellite map'. (I will be adding other items to the menu in the future)

THanks to anyone who can help with this


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.

Making an android map menu to change map type

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();
}


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>


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜