我正在创建一个Point功能,如下所示:
const myFeature = {
"geometry": {"coordinates": [position[0], position[1]], "type": "Point"},
"type": "Feature",
"id": 'my-point-feature'
}
this.geoJSONObject['features'].push(myFeature)
这很完美。
但是当我这样做时:
const myFeature = new Feature(new Point([position[0], position[1]]))
myFeature.setId('my-point-feature')
this.geoJSONObject['features'].push(myFeature)
我收到错误消息:
未捕获的TypeError:geometryReader不是函数 在Function._ol_format_GeoJSON_.readGeometry_(geojson.js:78) 在_ol_format_GeoJSON_.readFeatureFromObject(geojson.js:382) 位于_ol_format_GeoJSON_.readFeaturesFromObject(geojson.js:415) 在_ol_format_GeoJSON _._ ol_format_JSONFeature_.readFeatures(jsonfeature.js:60)
为什么这种行为上的差异?
答案 0 :(得分:1)
GeoJSON功能和OpenLayers功能是不同的对象类型,请使用new GeoJSON().writeFeatureObject
和new GeoJSON().readFeature
在格式之间进行转换:
const myFeature = new Feature(new Point([position[0], position[1]]))
myFeature.setId('my-point-feature')
const gjFeature = new GeoJSON().writeFeatureObject(myFeature)
this.geoJSONObject['features'].push(gjFeature)