Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
580 views
in Technique[技术] by (71.8m points)

view - Changing StreetView<->Satellite Google Maps Android

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...