OpenLayers – How to Fix Error: getGeometry is not a Function

openlayers

I have a GeoJSON file, I can select one of its objects and convert it to a feature object, I need to zoom to this polygon on the map so here is my code:
part of JSON object that I convert:

{ "type": "Feature", "properties": { "NAME": "TORSHAVN" }, 
    "geometry": { "type": "Polygon", "coordinates": 
        [ [ [ -6.758638858795166, 62.015167236328125 ],
        ................

Here is my code :

changJsonToFeaturLayer(jsonFeature, cb) {
    var geojsonFormat = new GeoJSON();
    var features = geojsonFormat.readFeatures(jsonFeature);
    cb(features)
}
onChange = value => {
    this.setState({
            selectedFeature: this.state.data[value]
        },
        () => this.changJsonToFeaturLayer(this.state.selectedFeature,
            (feature) => {
                console.log(feature) // log the feature successfully 
                const extent = feature.getGeometry().getExtent() //error getGeometry() is not a function 
                map.getView().fit(extent);
            }))
}

Best Answer

I think the problem is that readFeatures return an array of features, and you are passing to a function that expects one feature. That is probably why it is telling you that getGeometry is not a function .. of array

OL API Doc - GeoJSON readFeatures

Related Question