首页 > 解决方案 > Openlayers 功能创建问题

问题描述

我正在创建一个点功能,如下所示:

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 不是位于 _ol_format_GeoJSON_.readFeaturesFromObject (geojson.js:415) 处 _ol_format_GeoJSON_.readFeatureFromObject (geojson.js:382) 处的 Function._ol_format_GeoJSON_.readGeometry_ (geojson.js:78) 处的函数 ( jsonfeature.js:60)

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

标签: openlayers

解决方案


GeoJSON 功能和 OpenLayers 功能是不同的对象类型,使用new GeoJSON().writeFeatureObjectnew 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)

推荐阅读