7

A GeoJson feature looks like this :

{
  "type": "Feature",
  "properties": {},
  "geometry": {
    "type": "Point",
    "coordinates": [
      43.59375,
      59.17592824927136
    ]
  }
}

In Mapbox using Java/JVM we can construct the feature like this :

val testFeature = Feature.fromGeometry(Point.fromLngLat(2.0,3.0))

But I don't seem to find a method to get coordinates/point back from the feature.

There is a Feature#getGeometry() but I can't get the coordinates from that either as that's just a sugar for the GeoJson interface itself.

halfer
  • 19,824
  • 17
  • 99
  • 186
erluxman
  • 18,155
  • 20
  • 92
  • 126

2 Answers2

7

I just found that Each Feature expose the method .geometry() which we can cast to any type (Point, Line, polygon, Multipoit.. etc). From there we can get the either Point or List<Point>.

Example :

val position1 = feature1.geometry() as Point
val longitude = position1.longitude()

val area1 = feature2.geometry() as MultiPoint
val firstPointLatitude = area1.coordinates()!![0].latitude()
erluxman
  • 18,155
  • 20
  • 92
  • 126
1

Each feature has a .coordinates() method that returns a List<Point> or List<List<Point> object (unless you're calling it on a Point feature, in which case it will return a List<Double>.

[source: core API's geojson documentation]

riastrad
  • 1,624
  • 10
  • 22
  • 1
    [Feature.class](https://docs.mapbox.com/archive/android/java/api/libjava-geojson/4.3.0/com/mapbox/geojson/Feature.html) doesn't have a `.coordinates()` method though — you'd have to call `feature.geometry()` and then cast to a specific feature type first – anotherdave Feb 26 '21 at 10:03