Google Maps Android从GeoJsonFeature获取LatLng点数组

时间:2018-03-02 18:28:03

标签: android google-maps google-maps-android-api-2

如何在点击geojson功能的行时获得点列表?我需要访问每一行的各个点?这不起作用:lineStringFeature.getPolylineOptions()。getPoints()。非常感谢你。

// Set a listener for geometry clicked events.
layer.setOnFeatureClickListener(new GeoJsonLayer.OnFeatureClickListener() {
    @Override
    public void onFeatureClick(Feature feature) {
        GeoJsonFeature lineStringFeature;
        GeoJsonLineStringStyle lineStringStyle = new GeoJsonLineStringStyle();
        lineStringFeature = (GeoJsonFeature) feature;
        lineStringStyle.setColor(Color.GREEN);
        lineStringStyle.setZIndex(10f);
        lineStringStyle.setWidth(6f);
        lineStringFeature.setLineStringStyle(lineStringStyle);

        // this doesn't work how do you get an array of LatLngs
        lineStringFeature.getPolylineOptions().getPoints().get(0);
    }
}

1 个答案:

答案 0 :(得分:0)

GeoJsonFeature对象有一个返回几何的getGeometry()方法(如果您确定类型,则可以将其强制转换为GeoJsonLineString)和GeoJsonLineString对象有getCoordinates()方法返回List<LatLng>坐标:

if ("LineString".equalsIgnoreCase(lineStringFeature.getGeometry().getType())) {
    List<LatLng> coordinates = ((GeoJsonLineString) lineStringFeature.getGeometry()).getCoordinates();
    // Do something with the coordinates
} else if ("MultiLineString".equalsIgnoreCase(lineStringFeature.getGeometry().getType())) {
    for (GeoJsonLineString linestring : ((GeoJsonMultilineString) lineStringFeature.getGeometry()).getLineStrings()) {
        List<LatLng> coordinates = linestring.getCoordinates();
        // Do something with the coordinates. Take into account that one MultiLinestring is composed of several Linestrings
    }
}