当在地图android studio中添加标记时,我只能输入标题和摘要。
如何添加其他参数,例如id?
我稍后可以在单击标记时得到。
private void addMarker(LatLng latlng, final String title, final String deskripsi, final String id) {
markerOptions.position(latlng);
markerOptions.title(title);
markerOptions.snippet(deskripsi);
gMap.addMarker(markerOptions);
gMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
@Override
public void onInfoWindowClick(Marker marker) {
Toast.makeText(getApplicationContext(), marker.getTitle(), Toast.LENGTH_SHORT).show();
}
});
}
最佳答案
使用一个Hashmap来存储标记和ID
private HashMap<Marker, Integer> markerIdMapping = new HashMap<>();
private void addMarker(LatLng latlng, final String title, final String deskripsi, final String id) {
markerOptions.position(latlng);
markerOptions.title(title);
markerOptions.snippet(deskripsi);
Marker marker = gMap.addMarker(markerOptions);
markerIdMapping.put(marker, id);
gMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
@Override
public void onInfoWindowClick(Marker marker) {
Toast.makeText(getApplicationContext(), marker.getTitle(), Toast.LENGTH_SHORT).show();
String markerId = markerIdMapping.get(marker);
}
});
}