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 = 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)
为什么会有这种行为差异?
解决方案
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)
推荐阅读
- java - 在 Java 中操作 UTF8 字符串的方法(而不是转换为 UTF16)?
- java - 使用 read() 时 BufferReader 卡住
- node.js - 信任自签名证书 | 表示
- encryption - 无法通过 ARM 模板将 CMK 加密应用于 Azure 存储帐户
- node.js - 在 Web 应用程序身份验证中集成外部指纹传感器
- python - 从另一个数据帧中删除数据帧,该数据帧是第一个数据帧的过滤子集
- python - 您可以在 Windows 上运行 Sagemaker Python SDK 吗?
- sql - 在 Postgres 函数中使用的条件 IN 语句
- visual-studio-code - 如何在标签组之间同步搜索词?
- python-3.x - 使用多字符分隔符将数据帧保存到文件