2017-09-21 44 views
-3

我正在開發一個地圖應用程序在Android上尋找最好的道路。 問題並非所有的道路名稱都在谷歌API上,所以我有一個JSon文件。 這是擺脫JSON文件座標和顯示它與Android應用 從位置A線B.從座標json和谷歌地圖上搜索(Android)

的最佳方式
{ 
    "type": "FeatureCollection", 
    "crs": { 
     "type": "name", 
     "properties": { 
     "name": "urn:ogc:def:crs:OGC:1.3:CRS84" 
     } 
    }, 
    "features": [ 
     { 
     "type": "Feature", 
     "properties": { 
      "Name": "po", 
      "description": "", 
      "timestamp": null, 
      "begin": null, 
      "end": null, 
      "altitudeMode": null, 
      "tessellate": -1, 
      "extrude": 0, 
      "visibility": -1, 
      "drawOrder": null, 
      "icon": null, 
      "description_1": null, 
      "Number": "10", 
      "RoadNameCo": "03_10234", 
      "RoadNameAL": "MEHDI HOXHA" 
     }, 
     "geometry": { 
      "type": "Point", 
      "coordinates": [ 
       20.853835, 
       42.601668, 
       0 
      ] 
     } 
     }, 
     { 
     "type": "Feature", 
     "properties": { 
      "Name": "po", 
      "description": "", 
      "timestamp": null, 
      "begin": null, 
      "end": null, 
      "altitudeMode": null, 
      "tessellate": -1, 
      "extrude": 0, 
      "visibility": -1, 
      "drawOrder": null, 
      "icon": null, 
      "description_1": null, 
      "Number": "16", 
      "RoadNameCo": "03_10234", 
      "RoadNameAL": "MEHDI HOXHA" 
     }, 
     "geometry": { 
      "type": "Point", 
      "coordinates": [ 
       20.854006, 
       42.60127, 
       0 
      ] 
     } 
     } 

    ] 
} 
+0

究竟是什麼問題?你不能解析JSON,或者你不能在地圖上路線?這是兩個不同的問題,每個問題在這裏已經回答了幾百次。 –

+0

不知道如何解析JSON到谷歌地圖? –

+0

你無法解析JSON到谷歌地圖。正如我所說,這是兩個不同的問題。首先將JSON解析爲座標,然後將座標設置爲Google地圖。那麼2 **不同和完全無關的**任務是你的問題? –

回答

1

試試這個

private List<LatLng> getLocationList(String JSON_String) 
{ 
    List<LatLng> locationList = new ArrayList<>(); 
    try { 

     JSONObject object = new JSONObject(JSON_String); 
     JSONArray mArray = object.getJSONArray("features"); 

     for(int i=0;i<mArray.length();i++){ 

      JSONObject obj=mArray.getJSONObject(i); 
      JSONObject geometryObject=obj.getJSONObject("geometry"); 
      JSONArray locationJSON_Array=geometryObject.getJSONArray("coordinates"); 

      LatLng location=new LatLng(locationJSON_Array.getDouble(0),locationJSON_Array.getDouble(1)); 
      locationList.add(location); 
     } 

    }catch (JSONException ex){ 
     ex.printStackTrace(); 
     //handle Exception 
    } 
    return locationList; 
} 

使用方法如下

List<LatLng> LocationList =getLocationList(Your_JSON_String);//The String posted in question 
LatLng firstLocation=locationList.get(0); 
LatLng secondLocation=locationList.get(1); 

讓我知道是否有什麼問題

1

只是嘗試分析是這樣的:

JSONArray lat_long_jsonArray = jsonObject.getJSONArray("coordinates"); 
if (lat_long_jsonArray.length() != 0) 
{ 

    String Longitude(lat_long_jsonArray.get(0).toString()); 

    String Latitude(lat_long_jsonArray.get(1).toString()); 
} 
+0

請檢查您的代碼,它不會編譯 –

+0

@VladMatvienko我只給出瞭如何獲得合作伙伴關係,座標表格座標JsonArray現在你必須解析。 – yash786

+0

那不是我問過問題的人,而是你的代碼無效。請仔細檢查緯度和長度聲明,或使用情況,或任何它應該是,它不清楚。 –