【问题标题】:Take Coordinates from json and search on Google map (Android) [duplicate]从json获取坐标并在谷歌地图(Android)上搜索[重复]
【发布时间】:2018-03-02 10:25:57
【问题描述】:

我正在 Android 上开发一个地图应用程序,用于寻找最佳道路。 问题不是所有道路名称都在 Google API 上,所以我将它放在一个 JSon 文件中。 这是从 JSon 文件中获取坐标并将其显示给 Android 应用程序的最佳方法 从位置 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
            ]
         }
      }

   ]
}

【问题讨论】:

  • 究竟是什么问题?你不能解析 JSON,或者你不能在地图上做路线?这是 2 个不同的问题,每个问题都已经在这里回答了数百次。
  • 不知道如何将 json 解析为谷歌地图?
  • 您无法将 JSON 解析为谷歌地图。正如我所说,这是两个不同的问题。首先将 JSON 解析为坐标,然后将坐标设置为谷歌地图。那么您的问题是 2 个不同且完全不相关的任务中的哪一个?
  • 好的,谢谢或您的帮助。 l 如何将JSON解析为坐标?
  • each of which was answered here already for few hundred times - 搜索Android parse json,你会找到100多个答案

标签: android json google-maps


【解决方案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);

如果有什么问题请告诉我

【讨论】:

    【解决方案2】:

    试着像这样解析:

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

    【讨论】:

    • 请检查你的代码,它不会编译
    • @VladMatvienko 我只给了你如何从坐标 JsonArray 中获取坐标,现在你必须解析。
    • 提问的不是我,但您的代码无效。请仔细检查 Lat 和 Long 声明,或者用法,或者它应该是什么,不清楚。
    • @MuharremSmakiqi,对不起,我不想要一个。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-17
    • 2021-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多