Changing StreetView<->Satellite Google Maps And

2020-02-06 05:07发布

问题:

i wanna switch my GoogleMaps view between StreetView and Satellite via menubutton.

Here's my code:

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

Won't work.. what do i wrong?

Thanks, prexx

回答1:

When you say it doesn't work, we really need more info to try and help you! Does it crash, does it stay on Street/Sat View or just normal map etc, try to give more info and if it crashed post a copy of the logcat.

I think all you are missing is the line:

(EDIT: I just tried it without calling invalidate and it works so it must be the menu button ID's)

mapView.invalidate();

You need to call this in order for the mapView to refresh itself, so call it every time you change the mapView settings.


If that doesnt work then it may be your id's for the buttons arent recognised in your switch so try setting up your menu as an xml file int res/menu/ like:

<?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>

Then modify your code to:

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


回答2:

Dont use MapView. Use GoogleMap and do

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