openlayers - 添加了地图图层。没有错误。图层不可见
问题描述
我使用以下代码生成地图图层:
var GeoJSON = {};
GeoJSON.type = "FeatureCollection";
GeoJSON.features = [];
var iconStyle = new ol.style.Style({
image: new ol.style.Icon(({
anchor: [0.5, 46],
anchorXUnits: 'fraction',
anchorYUnits: 'pixels',
opacity: 0.75,
src: "~/icons/delivery-truck.png"
}))
});
for (var i = 0; i < data.length; i++) {
var machineGeoObject = {
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [data[i]["longitude"], data[i]["latitude"]],
},
"properties": data[i],
"style": iconStyle
}
GeoJSON.features.push(machineGeoObject);
} //end of loop
var format = new ol.format.GeoJSON({
featureProjection: "EPSG:3857"
});
var vector = new ol.layer.Vector({
source: new ol.source.Vector({
features: format.readFeatures(GeoJSON)
})
});
map.addLayer(vector);
没有错误。当我map.getLayers()
在控制台中调用时,我看到了添加的层。图层的属性“可见”为真。为什么我在地图上看不到带有图标的精确位置?为什么我只看到一张光秃秃的地图?
解决方案
您可能在 OpenLayers 默认样式的大西洋中 lon/lat 0,0 的 180 米范围内可以看到特征。您不能通过 GeoJSON 设置 OpenLayers 样式,它必须设置在图层上,或者使用setStyle()
. 此外featureProjection
,该格式仅用于图层源设置,如果使用readFeatures
必须在此处指定:
var GeoJSON = {};
GeoJSON.type = "FeatureCollection";
GeoJSON.features = [];
var iconStyle = new ol.style.Style({
image: new ol.style.Icon(({
anchor: [0.5, 46],
anchorXUnits: 'fraction',
anchorYUnits: 'pixels',
opacity: 0.75,
src: "~/icons/delivery-truck.png"
}))
});
for (var i = 0; i < data.length; i++) {
var machineGeoObject = {
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [data[i]["longitude"], data[i]["latitude"]],
},
"properties": data[i],
}
GeoJSON.features.push(machineGeoObject);
} //end of loop
var format = new ol.format.GeoJSON();
var vector = new ol.layer.Vector({
source: new ol.source.Vector({
features: format.readFeatures(GeoJSON, {
featureProjection: "EPSG:3857"
})
}),
style: iconStyle
});
还要检查您的图标src
网址是否正确(应该~
是.
或..
?)并确保data[i]["longitude"]
和data[i]["latitude"]
是数字而不是字符串(Number()
必要时使用)。
推荐阅读
- components - React 组件函数式
- ios - iOS ReactNative React 依赖问题
- python - 如何将两个文本文件合并为一个?
- gradle - 无法实例化“InvalidJavadocPosition”类
- c# - 将 2 个 ienumerable 与不同类型结合起来(一对多)
- azure - 从已部署的 WCF 服务中获取 404 但可以获取服务定义
- android - 如何将 Camera2Config.Extender 与 CameraX 一起使用
- android - 将 DialogFragment 放在所有活动的前面(即使是在显示 DialogFragment 之后创建的活动)
- php - 更改 wordpress 小部件内的默认 HTML 标记
- python - 使用 dict.values() 迭代时无法替换 dicts 列表中的字符串值