How do I get coordinates from geoJson?

I'm having difficulty getting coordinates from a GeoJson file. I managed to draw a Polygon on the map, but I need to get a coordinate sheet to measure the length of the route and add markers. Here is my geoJson file

{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "properties": {"scalerank":0,"featurecla":"Admin-0 country","labelrank":2,"sovereignt":"Russia","sov_a3":"RUS","adm0_dif":0,"level":2,"type":"Sovereign country","admin":"Russia","adm0_a3":"RUS","geou_dif":0,"geounit":"Russia","gu_a3":"RUS","su_dif":0,"subunit":"Russia","su_a3":"RUS","brk_diff":0,"name":"Russia","name_long":"Russian Federation","brk_a3":"RUS","brk_name":"Russia","brk_group":null,"abbrev":"Rus.","postal":"RUS","formal_en":"Russian Federation","formal_fr":null,"note_adm0":null,"note_brk":null,"name_sort":"Russian Federation","name_alt":null,"mapcolor7":2,"mapcolor8":5,"mapcolor9":7,"mapcolor13":7,"pop_est":140041247,"gdp_md_est":2266000,"pop_year":-99,"lastcensus":2010,"gdp_year":-99,"economy":"3. Emerging region: BRIC","income_grp":"3. Upper middle income","wikipedia":-99,"fips_10_":"RS","iso_a2":"RU","iso_a3":"RUS","iso_n3":"643","un_a3":"643","wb_a2":"RU","wb_a3":"RUS","woe_id":23424936,"woe_id_eh":23424936,"woe_note":"Exact WOE match as country","adm0_a3_is":"RUS","adm0_a3_us":"RUS","adm0_a3_un":-99,"adm0_a3_wb":-99,"continent":"Europe","region_un":"Europe","subregion":"Eastern Europe","region_wb":"Europe & Central Asia","name_len":6,"long_len":18,"abbrev_len":4,"tiny":-99,"homepart":1,"filename":"RUS.geojson"},
      "geometry": {
        "type": "MultiPolygon",
        "coordinates": [[[[132.44898522200018,42.845404364],[132.44988040500007,42.82811107000001],[132.4674585300002,42.81093984600007],[132.47771243600013,42.803900458],[132.47559655000012,42.80023834800015],[132.46713300900015,42.79682038000014],[132.458262566,42.798285223],[132.45337975400005,42.80097077000009],[132.44727623800017,42.80731842700011],[132.43946373800006,42.81753164300015],[132.4298608730002,42.82648346600017],[132.41749108200023,42.83002350500006],[132.4064233730002,42.82876211100013],[132

it turned out to go down only to geometry

Upvotes: 0

Views: 1150

Answers (1)

Milad Mohammadi
Milad Mohammadi

Reputation: 54

There are 2 common ways to do this.

  1. You can create a model for your response data as a data class using the "JSON To Kotlin Class" plugin in Android Studio. then easily you can access the JSON property as a Kotlin object.

  2. You have the JSON data as an string. add this line to your module-level build.gradle file:

dependencies {
  //...

  implementation 'com.google.code.gson:gson:2.8.9'
}

Then you can do parse your JSON String using Gson and get the coordinates as bellow:

val jsonData = "..."
val gson = Gson()
val coordinates = gson.fromJson(jsonData.getJSONArray("features")[0].getJSONObject("geometry").getJSONArray("coordinates").toString(), Array<Type /*your data type*/>::class.java).toList()

Upvotes: 1

Related Questions