Openlayers功能创建问题

Openlayers功能创建问题,openlayers,Openlayers,我正在创建一个点要素,如下所示: const myFeature = { "geometry": {"coordinates": [position[0], position[1]], "type": "Point"}, "type": "Feature", "id": 'my-point-feature' } this.geoJSONObject['features'].push(myFeature) 这很好用 但当我这么做的时候: const myFeature

我正在创建一个点要素,如下所示:

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)
我得到一个错误:

未捕获类型错误:geometryReader不是函数 at Function._ol_format_GeoJSON_.readGeometry_(GeoJSON.js:78) at_ol_format_GeoJSON_.readFeatureFromObject(GeoJSON.js:382) at_ol_格式_GeoJSON_.readFeaturesFromObject(GeoJSON.js:415) at_ol_format_GeoJSON_._ol_format_JSONFeature_.readFeatures(JSONFeature.js:60)


为什么会有这种行为差异?

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)

谢谢你的回复。这有帮助。我认为writeFeaturesObject是writeFeatureObject,因为我们在这里处理的是单个对象。再次感谢。是的,writeFeaturesObject将返回完整的GeoJSON