Ich analysiere JSON von Server-Antwort und setzen Markierungen auf Google Maps V2 auf Android. Um das Snippet anzuzeigen, verwende ich ein benutzerdefiniertes Layout mit HTML-Code. Mein Problem, dass Marker richtig setzen, aber setInfoWindowAdapter
sind Daten nur vom ersten JSON-Element erhalten und dann nicht aktualisieren, so bekomme ich die gleichen Informationen, wenn Sie auf einen der Marker klicken. Hier ist meine Funktion:setInfoWindowAdapter ist nicht erfrischend in für() Schleife
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");
}
Wow, das war wirklich über meine Java-Fähigkeiten. Vielen Dank! –
Ich bin froh, dass es geholfen hat :) – FlyingPumba