【发布时间】:2020-12-26 06:59:19
【问题描述】:
我在初始化图层时尝试向我的矢量图层添加自定义属性,因为源没有我需要的所有数据。
如果我对 API 文档的理解正确,应该可以,但我不知道在地图上的 VectorLayer 上选择要素时如何返回自定义属性的值。
我正在使用以下代码初始化 VectorLayer:
const someVectorLayer = new VectorLayer({
source: new VectorSource({
format: new GeoJSON(),
url: "https://example.com/api/v1/foobar",
}),
customProperty: 'foobar'
});
我正在尝试访问该属性以设置我的 fetch url,以便在单击该功能时从正确的位置获取内容:
function onSelect(e) {
const featureValues = e.target.getFeatures().getArray()[0];
const featureId = featureValues.get('id');
const featureCustomProperty = featureValues.get("customProperty");
const fetchUrl = `https://example.com/api/v1/${featureCustomProperty}/${featureId}`;
fetch(fetchUrl)
.then((response) => response.json())
.then((data) => console.log(data));
}
感谢我在该主题上获得的任何帮助。显然我还不是 Openlayers 专家。 :)
【问题讨论】:
标签: javascript openlayers