Openlayers 添加了地图图层。没有错误。图层不可见
我使用以下代码生成贴图层:Openlayers 添加了地图图层。没有错误。图层不可见,openlayers,openlayers-5,Openlayers,Openlayers 5,我使用以下代码生成贴图层: 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: 'pixel
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);
var GeoJSON={};
GeoJSON.type=“FeatureCollection”;
GeoJSON.features=[];
var iconStyle=新的ol.style.style({
图片:新ol.style.Icon(({
主播:[0.5,46],
主播:“分数”,
anchorYUnits:'像素',
不透明度:0.75,
src:“~/icons/delivery truck.png”
}))
});
对于(变量i=0;i
没有错误。在控制台中调用
map.getLayers()
时,我看到添加的层。图层的属性“可见”为真。为什么我看不到地图上有图标的精确位置?为什么我只看到一张光秃秃的地图?在OpenLayers默认样式的大西洋样式中,在lon/lat 0,0的180米范围内,可能有可见的特征。不能通过GeoJSON设置OpenLayers样式,必须在图层上或使用setStyle()
在功能上设置OpenLayers样式。此外,格式上的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
});
var GeoJSON={};
GeoJSON.type=“FeatureCollection”;
GeoJSON.features=[];
var iconStyle=新的ol.style.style({
图片:新ol.style.Icon(({
主播:[0.5,46],
主播:“分数”,
anchorYUnits:'像素',
不透明度:0.75,
src:“~/icons/delivery truck.png”
}))
});
对于(变量i=0;i
还要检查您的图标src
url是否正确(应该~
是
还是。
?),并确保数据[i][“经度”
和数据[i][“纬度”]
是数字而不是字符串(必要时使用Number()
)