Android Google在“信息窗口"中以不同的值映射标记 [英] Android google maps markers with different values on Info Window
问题描述
我有一个带有很多标记的地图,并且我希望在每个标记中为信息窗口"自定义值.
I have a map with a lot of markers, and I want custom values for the Info Window in each marker.
用于添加标记:
final JSONArray stops = new File().getStops(lineId);
try {
for (int i = 0; i < stops.length(); i++) {
BitmapDescriptor stopMarkerIcon = new File().getStopMarkerIcon(color, Integer.parseInt(stops.getJSONObject(i).getString("sequence")));
LatLng coordinate = new LatLng(Double.parseDouble(stops.getJSONObject(i).getString("lat")), Double.parseDouble(stops.getJSONObject(i).getString("lng")));
MarkerOptions markerOptions = new MarkerOptions().icon(stopMarkerIcon).position(coordinate).anchor(.5f, .5f);
googleMap.setInfoWindowAdapter(new StopsInfoWindow(i));
googleMap.addMarker(markerOptions);
}
} catch (JSONException e) { e.printStackTrace(); }
信息窗口适配器:
public class StopsInfoWindow implements GoogleMap.InfoWindowAdapter {
private View view;
private int i;
public StopsInfoWindow(int i) {
final LayoutInflater inflater = (LayoutInflater) Controller.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.item_stop_marker_info, null);
this.i = i;
}
@Override
public View getInfoContents(Marker marker) {
return null;
}
@Override
public View getInfoWindow(final Marker marker) {
TextView tvLat = (TextView) view.findViewById(R.id.tv_lat);
TextView tvLng = (TextView) view.findViewById(R.id.tv_lng);
tvLat.setText("Latitude:" + i);
tvLng.setText("Longitude:" + i);
return view;
}
}
问题是当我单击任何标记时,所有标记都显示相同的值.在这种情况下,"i"是stops.length()-1;
The problem is when I click any marker, it shows with the same value for all. In this case "i" being the stops.length() - 1;
我认为问题出在这里
googleMap.setInfoWindowAdapter(new StopsInfoWindow(i));
每次添加标记(每次在for循环中迭代)时,我都会在地图上设置一个新的信息窗口.有什么方法可以将适配器添加到标记本身而不是地图?这样,我可以创建一个新的InfoWindow,并为每个标记提供自定义值.
Each time I add a marker (each time I iterate in the for loop) I am setting a new Info Window to the map. Is there any way to add the Adapter to the marker itself instead of the map? That way I could have a new InfoWindow with custom values for each marker.
推荐答案
使用HashMap
我不确定这是否是最佳解决方案,但它是否有效.基本上,我只是将标记添加到HashMap中,然后将此HashMap传递给我的自定义信息窗口"类.
I'm not sure if this is the best solution but it works. Basically I just added the marker into a HashMap and passed this HashMap to my Custom Info Window class.
添加标记:
HashMap<Marker, JSONObject> stopsMarkersInfo = new HashMap<>(); // created the HashMap
JSONArray stops = new File().getStops(lineId);
for (int i = 0; i < stops.length(); i++) {
BitmapDescriptor stopMarkerIcon = new File().getStopMarkerIcon(color, Integer.parseInt(stops.getJSONObject(i).getString("sequence")));
LatLng coordinate = new LatLng(Double.parseDouble(stops.getJSONObject(i).getString("lat")), Double.parseDouble(stops.getJSONObject(i).getString("lng")));
MarkerOptions markerOptions = new MarkerOptions().icon(stopMarkerIcon).position(coordinate).anchor(.5f, .5f);
Marker marker = googleMap.addMarker(markerOptions);
stopsMarkersInfo.put(marker, stops.getJSONObject(i)); // added each marker and
// his information to the HashMap
}
googleMap.setInfoWindowAdapter(new StopsInfoWindow(stopsMarkersInfo)); // passed the HashMap
信息窗口适配器:
public class StopsInfoWindow implements GoogleMap.InfoWindowAdapter {
private HashMap<Marker, JSONObject> stopsMarkersInfo;
private View view;
public StopsInfoWindow(HashMap<Marker, JSONObject> stopsMarkersInfo) {
this.stopsMarkersInfo = stopsMarkersInfo;
}
@Override
public View getInfoContents(Marker marker) {
return null;
}
@Override
public View getInfoWindow(final Marker marker) {
JSONObject stop = stopsMarkersInfo.get(marker);
if (stop != null) {
LayoutInflater inflater = (LayoutInflater) Controller.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.item_stop_marker_info, null);
TextView stopName = (TextView) view.findViewById(R.id.stop_name);
stopName.setText(stop.getString("name"));
TextView stopLine = (TextView) view.findViewById(R.id.stop_line);
stopLine.setText(stop.getString("line"));
}
return view;
}
}
这篇关于Android Google在“信息窗口"中以不同的值映射标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!