制作一个android地图菜单来改变地图类型

Ste*_*eve 7 android google-maps

我的Android应用程序中有一张地图.默认情况下,它会显示卫星视图,但我已将其关闭以仅显示道路地图视图.但是,我想知道如何构建一个菜单,所以当用户按下菜单按钮时,它会在底部显示一个带有"切换卫星地图"的部分.(我将来会在菜单中添加其他项目)

对任何可以帮助解决这个问题的人都感谢

eas*_*get 22

这是我的实现,适用于GoogleMaps API v2.它显示一个带有四个单选按钮的对话框,您可以从中选择一种地图类型.当前选择的地图类型也已被选中.

Android AlertDialog选择GoogleMaps MapType

此代码最好进入您保存地图的活动.在调用showMapTypeSelectorDialog()之前,只需确保启动并正确显示地图.我还建议使用Resource Strings作为标签.

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();
}
Run Code Online (Sandbox Code Playgroud)


ber*_*dev 0

只需将其添加到您的活动中即可:

  @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);
     }
  }
Run Code Online (Sandbox Code Playgroud)

这应该位于单独的 xml 文件中(可能是 /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>
Run Code Online (Sandbox Code Playgroud)