2011-06-24 56 views
2

我想通過menubutton在StreetView和Satellite之間切換我的GoogleMaps視圖。更改StreetView <->衛星谷歌地圖Android

這裏是我的代碼:

public boolean onCreateOptionsMenu(Menu menu){ 

    menu.add(0, 0, 0, "StreetView"); 
    menu.add(0, 0, 1, "Satellite"); 

    return true; 
} 

public boolean onOptionsItemSelected (MenuItem item){ 

    switch (item.getItemId()){ 
     case 0: 
      mapView.setStreetView(true); 
     return true; 

     case 1 : 
      mapView.setSatellite(true); 
     return true; 

    } 

    return false; 
} 

不會工作..我該怎麼錯了?

感謝, prexx

回答

7

當你說這是行不通的,我們真的需要更多的信息,試圖幫助你!它是否會崩潰,它留在街道/週六視圖或只是普通的地圖等,試圖給更多的信息,如果它墜毀logcat副本後。

我認爲你缺少的是行:

(編輯:我只是想它不調用無效和它的作品所以它必須是菜單按鈕的ID)

mapView.invalidate(); 

您需要爲了讓mapView自己刷新,請調用它,所以每次更改mapView設置時都要調用它。


如果那不工作,那麼它可能是你的ID的按鈕在你的交換機的arent承認這樣試試你的菜單設置爲XML文件INT RES /菜單/像:

<?xml version="1.0" encoding="utf-8"?> 
<menu 
    xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:title="Street View" android:numericShortcut="1" android:id="@+id/mapStreet" ></item> 
    <item android:title="Sat View" android:numericShortcut="2" android:id="@+id/mapSat"></item> 
</menu> 

然後修改您的代碼爲:

public boolean onCreateOptionsMenu(Menu menu){ 
    super.onCreateOptionsMenu(menu); 
    MenuInflater oMenu = getMenuInflater(); 
    oMenu.inflate(R.menu.mapsmenu, menu); 
    return true; 
} 

public boolean onOptionsItemSelected(MenuItem item){ 
    switch(item.getItemId()){ 
    case R.id.mapStreet: 
     mapView.setStreetView(true); 
     mapView.setSatellite(false); 
     mapView.invalidate(); 
     return true; 

    case R.id.mapSat: 
     mapView.setSatellite(true); 
     mapView.setStreetView(false); 
     mapView.invalidate(); 
     return true; 
    } 
    return false; 
} 
+0

UFF ......我postet我的問題要快..你的第二個提示,設置菜單中的另一種方式。我只是「敬酒」--item.getItemId() - 並認出它在兩個menubuttons上都是0 ..我的錯在這裏:menu.add(0,1,1,「Satellite」);抱歉的傢伙..:/謝謝你kenny – Prexx

+0

使用xml設置你的菜單將使生活更輕鬆,因爲這些id都是已註冊的,如果你想添加一個額外的按鈕,它非常簡單。 – Kenny

5

不使用MapView。 使用GoogleMap的,做

GoogleMap map; 
map.setMapType(GoogleMap.MAP_TYPE_SATELLITE); 
+3

3年後,但反正。 – Prexx