【问题标题】:setInfoWindowAdapter is not refreshing in for() loopsetInfoWindowAdapter 在 for() 循环中不刷新
【发布时间】:2016-07-28 03:21:03
【问题描述】:

我正在从服务器响应中解析 JSON,并将标记放在 Android 上的 Google Maps V2 中。要显示 sn-p,我正在使用带有 HTML 代码的自定义布局。我的问题是标记放置正确,但setInfoWindowAdapter 仅从第一个 JSON 元素获取数据,然后不刷新,因此单击任何标记时我都会得到相同的信息。这是我的功能:

void createMarkersFromJson(String json) throws JSONException {
    // De-serialize the JSON string into an array of city objects
    JSONObject jObj = new JSONObject(json);
    jsonArray = jObj.getJSONArray("my_array");
    for (int i = 0; i < jsonArray.length(); i++) {
        // Create a marker for each city in the JSON data.

        JSONObject jsonObj = jsonArray.getJSONObject(i);
        time = new Date(jsonObj.getInt("time"));
        calendar.setTime(time);

        htmlString =
                "<div>\n" +
                "            <b>"+jsonObj.getString("name")+"</b>\n" +
                "            <span> - </span>\n" +
                "            <small>\n" +
                "                <a href='http://www.foo.bar/' target='_blank' title='FooBar'>" + "# " +
        jsonObj.getInt("id")+"</a>\n" +
                "            </small>\n" +
                "        </div>\n" +
                "        <div>\n" +
                        getString(R.string.dissapear) + " " + calendar.get(Calendar.HOUR)+":"+calendar.get(Calendar.MINUTE)+":"+calendar.get(Calendar.SECOND)+"\n</div>\n";
        mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {

            @Override
            public View getInfoWindow(Marker marker) {
                return null;
            }

            @Override
            public View getInfoContents(Marker marker) {
                View v = getLayoutInflater().inflate(R.layout.info_window_layout, null);
                Spanned spannedContent = Html.fromHtml(htmlString);
                TextView html = (TextView) v.findViewById(R.id.html);
                html.setText(spannedContent, TextView.BufferType.SPANNABLE);
                return v;
            }
        });
        MarkerOptions markerOptions = new MarkerOptions();
        markerOptions.position(new LatLng(
                        jsonObj.getDouble("latitude"),
                        jsonObj.getDouble("longitude")));
        markerOptions.icon(BitmapDescriptorFactory.fromAsset("icons/"+jsonObj.getString("id")+".png"));
        mMap.addMarker(markerOptions);

    }
    Log.e(JSON_TAG, "JSON sucessfully parsed");
}

【问题讨论】:

    标签: java android json google-maps maps


    【解决方案1】:

    问题出在这里:

            @Override
            public View getInfoContents(Marker marker) {
                View v = getLayoutInflater().inflate(R.layout.info_window_layout, null);
                Spanned spannedContent = Html.fromHtml(htmlString);
                TextView html = (TextView) v.findViewById(R.id.html);
                html.setText(spannedContent, TextView.BufferType.SPANNABLE);
                return v;
            }
    

    Map 只能有一个 InfoWindowAdapter。您正在使用它,就好像每个标记都有一个。 所以,在上面的方法中,无论参数传递的标记是什么,你总是使用相同的字符串。

    为了实现你想要的,你应该为每个标记构建字符串,并在末尾设置 InfoWindowAdapter。 像这样:

    Map<Marker, String> markers = new HashMap<Marker, String>();
    
    void createMarkersFromJson(String json) throws JSONException {
        // De-serialize the JSON string into an array of city objects
        JSONObject jObj = new JSONObject(json);
        jsonArray = jObj.getJSONArray("my_array");
        for (int i = 0; i < jsonArray.length(); i++) {
            // Create a marker for each city in the JSON data.
            JSONObject jsonObj = jsonArray.getJSONObject(i);
            time = new Date(jsonObj.getInt("time"));
            calendar.setTime(time);
    
            htmlString =
                    "<div>\n" +
                    "            <b>"+jsonObj.getString("name")+"</b>\n" +
                    "            <span> - </span>\n" +
                    "            <small>\n" +
                    "                <a href='http://www.foo.bar/' target='_blank' title='FooBar'>" + "# " +
            jsonObj.getInt("id")+"</a>\n" +
                    "            </small>\n" +
                    "        </div>\n" +
                    "        <div>\n" +
                            getString(R.string.dissapear) + " " + calendar.get(Calendar.HOUR)+":"+calendar.get(Calendar.MINUTE)+":"+calendar.get(Calendar.SECOND)+"\n</div>\n";
    
            MarkerOptions markerOptions = new MarkerOptions();
            markerOptions.position(new LatLng(
                            jsonObj.getDouble("latitude"),
                            jsonObj.getDouble("longitude")));
            markerOptions.icon(BitmapDescriptorFactory.fromAsset("icons/"+jsonObj.getString("id")+".png"));
            Marker marker = mMap.addMarker(markerOptions);
            markers.put(marker, htmlString);
        }
    
            mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
    
                @Override
                public View getInfoWindow(Marker marker) {
                    return null;
                }
    
                @Override
                public View getInfoContents(Marker marker) {
                    View v = getLayoutInflater().inflate(R.layout.info_window_layout, null);
                    Spanned spannedContent = Html.fromHtml(markers.get(marker));
                    TextView html = (TextView) v.findViewById(R.id.html);
                    html.setText(spannedContent, TextView.BufferType.SPANNABLE);
                    return v;
                }
            });
        Log.e(JSON_TAG, "JSON sucessfully parsed");
    }
    

    【讨论】:

    • 哇,这真的超出了我的 java 技能。非常感谢!
    • 我很高兴它有帮助:)
    猜你喜欢
    • 1970-01-01
    • 2017-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-09
    • 2011-09-26
    • 1970-01-01
    • 2011-11-07
    相关资源
    最近更新 更多