2016-07-28 10 views
1

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

Antwort

1

Das Problem ist hier:

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

Karte nur einen InfoWindowAdapter haben kann. Sie verwenden es so, als ob es für jeden Marker einen gibt. Also, in der obigen Methode, egal was ist der Marker von Parameter übergeben, verwenden Sie immer die gleiche Zeichenfolge.

Um zu erreichen, was Sie wollen, sollten Sie die Zeichenfolge für jeden Marker erstellen und den InfoWindowAdapter am Ende festlegen. Gefällt mir:

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"); 
} 
+0

Wow, das war wirklich über meine Java-Fähigkeiten. Vielen Dank! –

+0

Ich bin froh, dass es geholfen hat :) – FlyingPumba

Verwandte Themen