从json获取坐标并在Google地图上搜索(Android)

时间:2017-09-21 07:14:02

标签: android json google-maps

我正在Android上开发一个关于寻找最佳道路的地图应用程序。 问题不是所有道路名称都在Google API上,所以我在一个JSon文件上有它。 哪个是从JSon文件获取坐标并将其显示到Android App的最佳方法 从位置A到B的行。

    {
   "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
            ]
         }
      }

   ]
}

2 个答案:

答案 0 :(得分: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());
}

答案 1 :(得分: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);

让我知道是否有任何问题