【问题标题】:Android google maps markers with different values on Info WindowAndroid 谷歌地图标记在信息窗口上具有不同的值
【发布时间】: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


【解决方案1】:

使用 HashMap

我不确定这是否是最佳解决方案,但它确实有效。基本上我只是将标记添加到 HashMap 中,然后将此 HashMap 传递给我的自定义信息窗口类。

添加标记:

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;
    } 
}

【讨论】:

    猜你喜欢
    • 2011-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多