-3

I am developing a map application on Android which is about finding best roads. The problem is not all roads name are on Google API so i have it on one JSon file. Which is the best way to get coordinates from JSon file and show it to Android App with lines from position A to 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
            ]
         }
      }

   ]
}
MeliDev
  • 87
  • 11
  • what exactly is a problem? you can't parse JSON, or you can't do route on the map? That are 2 different questions, each of which was answered here already for few hundred times. – Vladyslav Matviienko Sep 21 '17 at 07:17
  • No idea how to parse json to google map? – MeliDev Sep 21 '17 at 07:18
  • you can't parse JSON to google map. As I said, that are 2 different questions. First parse JSON to coordinates, then set coordinates to google map. So what of 2 **different and completely unrelated** tasks is your question? – Vladyslav Matviienko Sep 21 '17 at 07:20
  • Alright thanks or your help. How do l parse JSON to coordinates? – MeliDev Sep 21 '17 at 07:24
  • `each of which was answered here already for few hundred times` - search for `Android parse json`, you will find 100+ answers – Vladyslav Matviienko Sep 21 '17 at 07:26
  • Not sure if this is an answer, but [check this out.](https://www.youtube.com/watch?v=CCZPUeY94MU) Its a tutorial to find road and add polylines on road. I tried and I can assure you it is working. If you want to know how it works (demo), check out [this part of video](https://youtu.be/CCZPUeY94MU?t=1447). It have English subtitles – Aswin P Ashok Sep 21 '17 at 08:08
  • @AswinPAshok thanks for help, the polyines part is OK but the coordinates lat long taking from json dont know? – MeliDev Sep 21 '17 at 08:11
  • So you just want the `"coordinates": [20.854006,42.60127,0]` out of that object? and store that in an array? – Aswin P Ashok Sep 21 '17 at 09:02
  • Yes only coordinates – MeliDev Sep 21 '17 at 09:03
  • do you want that 0 in array? I can write a function which return a list of LatLng objects from the string given without 0. – Aswin P Ashok Sep 21 '17 at 09:50
  • That will be helpful as well – MeliDev Sep 21 '17 at 10:16

2 Answers2

1

Just try to parse like this:

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());
}
Miriam Farber
  • 18,986
  • 14
  • 61
  • 76
yash786
  • 1,151
  • 8
  • 18
1

Try this

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

Use it like this

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

Let me Know if anything goes wrong

Aswin P Ashok
  • 702
  • 8
  • 27