避免使用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);
}
});