我正在从服务器响应中解析JSON并将标记放到Android上的Google Maps V2中。要显示片段我正在使用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");
}
答案 0 :(得分: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;
}
地图只能有一个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");
}