【发布时间】: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