2011-08-15 46 views
3

我在我的Android應用程序中有一張地圖。默認情況下,它顯示衛星視圖,但我已將其關閉以僅顯示路線圖視圖。但是,我想知道如何構建一個菜單,所以當用戶按下菜單按鈕時,它會在底部顯示「切換衛星地圖」部分。 (我會在將來添加其他項目到菜單)製作Android地圖菜單以更改地圖類型

由於任何人誰可以用這個

回答

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

這應該是一個獨立的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> 
0

建設的菜單/按鈕/標籤/你選,並在事件監聽器做:​​這將把衛星變成路線圖。乾杯。

18

這是我的實現,適用於GoogleMaps API v2。它顯示一個包含四個單選按鈕的對話框,您可以從中選擇地圖類型。當前選擇的地圖類型也已被選中。

Android AlertDialog to select GoogleMaps MapType

該代碼最好進入您的活動保存地圖。在調用showMapTypeSelectorDialog()之前,請確保您的地圖已啓動並正確顯示。我還建議使用資源字符串作爲標籤。

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

謝謝@easytarget你剛剛救了我的週末:) –