我想通过menubutton在StreetView和Satellite之间切换我的GoogleMaps视图.
这是我的代码:
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;
}
Run Code Online (Sandbox Code Playgroud)
不会工作..我错了什么?
谢谢,prexx
当你说它不起作用时,我们真的需要更多的信息来试着帮助你!它是否会崩溃,是停留在街道/卫星视图还是普通地图等,尝试提供更多信息,如果它崩溃后发布了logcat的副本.
我想你所缺少的就是这条线:
(编辑:我只是尝试了它而没有调用invalidate它工作,所以它必须是菜单按钮ID)
mapView.invalidate();
Run Code Online (Sandbox Code Playgroud)
您需要调用此方法以使mapView自行刷新,因此每次更改mapView设置时都要调用它.
如果这不起作用,那么你的id可能是因为按钮在你的开关中没有被识别,所以尝试将你的菜单设置为xml文件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>
Run Code Online (Sandbox Code Playgroud)
然后将代码修改为:
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;
}
Run Code Online (Sandbox Code Playgroud)
不要使用MapView.使用GoogleMap即可
GoogleMap map;
map.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13225 次 |
| 最近记录: |