从GeoJson获取坐标

时间:2018-11-09 11:46:40

标签: android geojson

我的应用程序中包含geojson图层。它正在动态加载。基本上,我需要向所有geojson功能添加标记,所以我需要“位置”。

我有这样的循环:

 for (GeoJsonFeature feature : layer.getFeatures()) {
                feature.setPolygonStyle(polygonStyle);   
            }

但是我不知道如何从要素中获取坐标。我已经尝试过这种方法:

private List<LatLng> getCoordinatesFromGeometry(Geometry geometry) {

        List<LatLng> coordinates = new ArrayList<>();

        // GeoJSON geometry types:
        // http://geojson.org/geojson-spec.html#geometry-objects

        switch (geometry.getGeometryType()) {
            case "Point":
                coordinates.add(((GeoJsonPoint) geometry).getCoordinates());
                break;
            case "MultiPoint":
                List<GeoJsonPoint> points = ((GeoJsonMultiPoint) geometry).getPoints();
                for (GeoJsonPoint point : points) {
                    coordinates.add(point.getCoordinates());
                }
                break;
            case "LineString":
                coordinates.addAll(((GeoJsonLineString) geometry).getCoordinates());
                break;
            case "MultiLineString":
                List<GeoJsonLineString> lines =
                        ((GeoJsonMultiLineString) geometry).getLineStrings();
                for (GeoJsonLineString line : lines) {
                    coordinates.addAll(line.getCoordinates());
                }
                break;
            case "Polygon":
                List<? extends List<LatLng>> lists =
                        ((GeoJsonPolygon) geometry).getCoordinates();
                for (List<LatLng> list : lists) {
                    coordinates.addAll(list);
                }
                break;
            case "MultiPolygon":
                List<GeoJsonPolygon> polygons =
                        ((GeoJsonMultiPolygon) geometry).getPolygons();
                for (GeoJsonPolygon polygon : polygons) {
                    for (List<LatLng> list : polygon.getCoordinates()) {
                        coordinates.addAll(list);
                    }
                }
                break;
        }

        return coordinates;
    }

但是似乎不起作用。有什么帮助吗?

1 个答案:

答案 0 :(得分:0)

这是我从JAVASCRIPT中的geojson中提取第一个坐标的方式

function getFirstCorrdinatesFromData(result){

    var firstCordinate = [];
      for (var i = 0; i < result.length; i++) {
        //alert(result[i].geometry.type)
        if(result[i].geometry.type == 'MultiPolygon') {
            
            let coordinates = result[i].geometry.coordinates;
            firstCordinate.push(coordinates[0][0][0]);


  }else if(result[i].geometry.type == 'Polygon') {
    let coordinates = result[i].geometry.coordinates;
            firstCordinate.push(coordinates[0][0]);
     
  }else if(result[i].geometry.type == 'Point') {
    let coordinates = result[i].geometry.coordinates;
            firstCordinate.push(coordinates);
     
  } 
      }
      return firstCordinate;
}
var arrayOfCoordinates=getFirstCorrdinatesFromData(data.features);

这就是我的json的样子:https://pastebin.com/80QDtt93