我正在尝试编写一个应用程序,以使用Google Maps V2显示预定义的位置。仅仅因为我必须与旧设备兼容,我使用了一些兼容性库。
我的问题是从不调用菜单选项。这是代码:
package jv.android.getpointlib;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.Polyline;
import com.google.android.gms.maps.model.PolylineOptions;
import jv.android.getpoint.R;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.content.Intent;
import android.graphics.Color;
import jv.android.utils.Message;
import com.google.android.gms.maps.SupportMapFragment;
//public class PointViewV2 extends FragmentActivity {
public class PointViewV2 extends FragmentActivity {
private GoogleMap map;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pointviewv2);
map = ((SupportMapFragment)(getSupportFragmentManager().findFragmentById(R.id.map))).getMap();
Intent intent = getIntent();
Double lat = 0D;
Double lon = 0D;
double[] lats = null;
double[] lons = null;
String nome = "";
if (intent != null)
{
Bundle params = intent.getExtras();
if (params != null) {
lat = (Double) params.getDouble("latitude");
lon = (Double) params.getDouble("longitude");
lats = (double[]) params.getDoubleArray("lats");
lons = (double[]) params.getDoubleArray("lons");
nome = params.getString("nome");
}
}
try {
if (lats == null || lons == null || lats.length == 0 || lons.length == 0) {
final LatLng point = new LatLng(lat, lon);
map.addMarker(new MarkerOptions().position(point).title(nome).icon(BitmapDescriptorFactory.fromResource(R.drawable.waypoint2)));
map.moveCamera(CameraUpdateFactory.newLatLngZoom(point, 5));
map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
} else {
map.addMarker(new MarkerOptions().position(new LatLng(lats[0], lons[0])).title(nome).icon(BitmapDescriptorFactory.fromResource(R.drawable.waypoint2)));
map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lats[0], lons[0]), 5));
map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
PolylineOptions path = new PolylineOptions();
for (int i = 0; i < lats.length; i++) {
path.add(new LatLng(lats[i], lons[i]));
}
path.color(Color.BLUE);
@SuppressWarnings("unused")
Polyline line = map.addPolyline(path);
}
}
catch (Exception e) {
Message.showMessage(PointViewV2.this, getString(R.string.aviso), getString(R.string.avErroAbrindoMaps), R.drawable.exclamation);
} /**/
((SupportMapFragment)(getSupportFragmentManager().findFragmentById(R.id.map))).setHasOptionsMenu(true);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.point_view_v2, menu);
return true;
/* //Log.i("MYINFO", "I'm in");
MenuItem mi0 = menu.add(Menu.NONE, 0, 0, getString(R.string.mnVerMapa));
mi0.setIcon(R.drawable.mapview);
MenuItem mi1 = menu.add(Menu.NONE, 1, 1, getString(R.string.mnVerSatelite));
mi1.setIcon(R.drawable.satelliteview);
MenuItem mi2 = menu.add(Menu.NONE, 2, 2, getString(R.string.mnVerHibrido));
mi2.setIcon(R.drawable.hybridview);
MenuItem mi3 = menu.add(Menu.NONE, 3, 3, getString(R.string.mnVerTerreno));
mi3.setIcon(R.drawable.terrainview);
return true; /**/
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.mnMap) {
map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
} else if (item.getItemId() == R.id.mnSatelite) {
map.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
} else if (item.getItemId() == R.id.mnHybrid) {
map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
} else if (item.getItemId() == R.id.mnHybrid) {
map.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
}
return true;
}
}
我的XML布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"/>
</LinearLayout>
菜单选项:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/mnMap"
android:orderInCategory="100"
android:showAsAction="always"
android:icon="@drawable/mapview"
android:title="@string/mnVerMapa"/>
<item
android:id="@+id/mnSatelite"
android:orderInCategory="100"
android:showAsAction="always"
android:icon="@drawable/satelliteview"
android:title="@string/mnVerSatelite"/>
<item
android:id="@+id/mnHybrid"
android:orderInCategory="100"
android:showAsAction="always"
android:icon="@drawable/hybridview"
android:title="@string/mnVerHibrido"/>
<item
android:id="@+id/mnTerrain"
android:orderInCategory="100"
android:showAsAction="always"
android:icon="@drawable/terrainview"
android:title="@string/mnVerTerreno"/>
</menu>
我可以看到地图,但缺少选项菜单:(
答案 0 :(得分:2)
尝试将onCreateOptionsMenu中的return语句从return true
更改为return super.onCreatOptionsMenu(menu)
。您应该更改onOptionsItemSelected的最后一个return语句以返回super。