【发布时间】:2016-09-16 09:31:01
【问题描述】:
我有一张带有很多标记的地图,我希望每个标记中的信息窗口都有自定义值。
添加标记:
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;
我认为问题出在这里:
googleMap.setInfoWindowAdapter(new StopsInfoWindow(i));
每次我添加一个标记(每次我在 for 循环中迭代)时,我都会为地图设置一个新的信息窗口。有没有办法将适配器添加到标记本身而不是地图?这样我就可以拥有一个新的 InfoWindow,其中包含每个标记的自定义值。
【问题讨论】:
标签: java android google-maps