2016-07-28 72 views
1

我正在解析服務器響應中的JSON,並將標記放到Android上的Google Maps V2中。要顯示代碼段,我使用HTML代碼的自定義佈局。我的問題是標記正確放置,但setInfoWindowAdapter只從第一個JSON元素獲取數據,然後不刷新,所以當點擊任何標記時,我得到相同的信息。這裏是我的功能:setInfoWindowAdapter在for()循環中不刷新

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

回答

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

哇,這真的超過了我的java技能。非常感謝你! –

+0

我很高興它幫助:) – FlyingPumba