blg2
blg2

Reputation: 385

Google Maps Android get LatLng array of points from GeoJsonFeature

How do I get a list of points when clicking on a line that is a geojson feature? I need to access the individual points of each line? This does not work: lineStringFeature.getPolylineOptions().getPoints(). Thank you very much.

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

Upvotes: 1

Views: 1315

Answers (1)

antonio
antonio

Reputation: 18262

The GeoJsonFeature object has a getGeometry() method that returns the geometry (you can cast it to GeoJsonLineString if you are sure of the type), and the GeoJsonLineString object has a getCoordinates() method that returns a List<LatLng> with the coordinates:

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

Upvotes: 1

Related Questions