避免使用Activity实现OnMarkerClickListener,使用本地的OnMarkerClickListener
// Not a good idea
class MapActivity extends Activity implements OnMarkerClickListener {
}
您将需要一张地图来查找链接到标记的原始数据模型
private Map<Marker, Map<String, Object>> markers = new HashMap<>();
您将需要一个数据模型
private Map<String, Object> dataModel = new HashMap<>();
将一些数据放入数据模型中
dataModel.put("title", "My Spot");
dataModel.put("snipet", "This is my spot!");
dataModel.put("latitude", 20.0f);
dataModel.put("longitude", 100.0f);
使用数据模型创建新标记时,将两者都添加到制造商地图
Marker marker = googleMap.addMarker(markerOptions);
markers.put(marker, dataModel);
对于单击标记事件,请使用本地 OnMarkerClickListener:
@Override
public void onMapReady(GoogleMap googleMap) {
// grab for laters
this.googleMap = googleMap;
googleMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker marker) {
Map dataModel = (Map)markers.get(marker);
String title = (String)dataModel.get("title");
markerOnClick(title);
return false;
}
});
mapView.onResume();
showMarkers();
ZoomAsync zoomAsync = new ZoomAsync();
zoomAsync.execute();
}
为了显示信息窗口,从标记图中检索原始数据模型:
@Override
public void onMapReady(GoogleMap googleMap) {
this.googleMap = googleMap;
googleMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
@Override
public void onInfoWindowClick(Marker marker) {
Map dataModel = (Map)markers.get(marker);
String title = (String)dataModel.get("title");
infoWindowOnClick(title);
}
});