2017-10-17 41 views
0

任何人都可以解釋如何在Android的谷歌地圖上顯示多個標記使用以下JSON代碼?如何在地圖上顯示多個標記

[ 
{"id":"1","lat":"18.105917","lng":"78.838325","location":" Laxmi Filling Station(Medak Road)","distance":"0"}, 
{"id":"3","lat":"18.105952","lng":"78.840366","location":"Fathima Filling Station (Medak Road)","distance":"0.1340667689594937"}, 
{"id":"13","lat":"18.093713","lng":"78.843775","location":"Thirumala Thirupathi Filling Station (Ensanpally Road)","distance":"0.9160924683870764"}, 
{"id":"12","lat":"18.101497","lng":"78.852353","location":"MS Balaji Filling Station (Old Busstand)","distance":"0.9706182480093937"} 
] 

所有的錯誤都解決仍然標記的沒有顯示在地圖上

private void createMarkersFromJson(String json) throws JSONException { 

// De-serialize the JSON string into an array of city objects 
JSONArray jsonArray = new JSONArray(json); 
for (int i = 0; i < jsonArray.length(); i++) { 
    // Create a marker for each city in the JSON data. 
    JSONObject jsonObj = jsonArray.getJSONObject(i); 
    String location=jsonObj.getString("location"); 
    double lat=jsonObj.getDouble("lat"); 
    double lng=jsonObj.getDouble("lng"); 
    String id=jsonObj.getString("id"); 
    Log.e("detailes", lat+" "+lng+" "+location); 
    map.addMarker(new MarkerOptions() 
      .title(location) 
      .snippet(id) 
      .position(new LatLng(lat, lng)) 
    ); 
} 
} 
+1

什麼你試過到目前爲止並編碼沒有爲你工作? –

+0

json數組不匹配異常是我得到的錯誤 –

+0

我認爲你犯了一個錯誤,以得到經緯度數組。檢查jsonObj.getJSONArray(「latlng」)和jsonObj.getJSONArray(「latlng」)。 –

回答

0

人口關鍵是不存在的JSON字符串

private void createMarkersFromJson(String json) throws JSONException { 
// De-serialize the JSON string into an array of city objects 
     JSONArray jsonArray = new JSONArray(json); 
     for (int i = 0; i < jsonArray.length(); i++) { 
// Create a marker for each city in the JSON data. 
      JSONObject jsonObj = jsonArray.getJSONObject(i); 
      map.addMarker(new MarkerOptions() 
        .title(jsonObj.getString("name")) 
        .snippet(jsonObj.getString("location")) 
        .position(new LatLng(
          jsonObj.getDouble("lat"), 
          jsonObj.getDouble("lng") 
        )) 
      ); 
     } 
    } 
+0

替換人口與位置,現在這將工作正常 –

+0

我已更改代碼,所有錯誤已解決仍然標記不顯示在地圖上 –

+0

只有當您的地圖準備就緒後,檢查您是否調用了此方法createMarkersFromJson()@RaghavaManchala –

1

請按照這種方式更改您的代碼。

for(int i=0;i<jsonArray.length();i++) 
    { 
     JSONObject jsonObject=jsonArray.getJSONObject(i); 
     String _id =jsonObject.getString("id"); 
     String _lat =jsonObject.getString("lat"); 
     String _lng =jsonObject.getString("lng"); 

    map.addMarker(new MarkerOptions() 
    .title(jsonObject.getString("name")) 
    .snippet(Integer.toString(jsonObject.getString("distance"))) 
    .position(new LatLng(_lat,_lng)); // If problem then convert String to double. Use getDouble() instead of getString() 

    } 
+1

我已更改代碼,所有錯誤均已解決仍然標記未顯示在地圖上 –

+0

http://hasmukhbhadani.blogspot.in/2015/08/display-multiple-markers -map-zoom-level.html –

+1

非常感謝你爲此.. 這可以幫助我很多..... IntelliJ Amiya –