【发布时间】:2020-10-16 03:00:36
【问题描述】:
我正在使用 OpenLayers 在地图上显示道路文件。目前,我设法根据道路类型分配样式,但是当我运行服务器时,我可以看到所有显示的数据。 有什么方法可以只显示主要道路(例如,当我运行服务器时,它应该只在地图上首先显示橙色道路,然后在缩放后它应该显示其他道路)当我运行服务器并且可以在缩放后看到更多的次要道路像谷歌地图或开放街道地图的显示方式?我尝试在我的代码中使用 minZoom,但它不起作用。还有如何在放大/缩小时动态改变笔画的粗细?
代码
var styleRoadFunction = function(feature) {
console.log(feature);
//assign symbology of roads based on road type
var color;
var outerColor;
var lightStroke;
var darkStroke;
//var maxZoom;
var minZoom;
if (feature.get("type")=='primary' && 'secondary' && 'trunk'){
color = 'rgba(252, 214, 112, 1)',
outerColor ='rgba(252, 214, 112, 1)',
lightStroke = 'null',
darkStroke = 'null';
//minZoom = 13.999;
} else if (feature.get("type")=='motorway'){
color = 'rgba(245, 171, 53, 1)',
outerColor ='rgba(245, 171, 53, 1)',
lightStroke = 'null',
darkStroke = 'null';
//minZoom = 12.999;
}else if (feature.get("type")=='cycleway'){
color = 'rgba(3, 166, 120, 1)',
outerColor = 'rgba(3, 166, 120, 1)',
lightStroke ='rgba(236, 240, 241, 1)',
darkStroke = 'rgba(3, 166, 120, 1)';
} else {
color = 'rgba(236, 236, 236, 1)',
outerColor = 'rgba(189, 195, 199, 1)',
lightStroke = 'null',
darkStroke = 'null';
//minZoom =3
//maxZoom= 7;
}
var retStyle = [new ol.style.Style({
stroke: new ol.style.Stroke({
color: outerColor,
lineCap: 'butt',
lineJoin:'round',
tolerence: 5,
width: 7,
//maxZoom: maxZoom,
//minZoom : minZoom,
opacity: 0,
zIndex: 0
})
}),
new ol.style.Style({
stroke: new ol.style.Stroke({
color: color,
lineCap:'butt',
lineJoin: 'round',
tolerence: 5,
// maxZoom: maxZoom,
//minZoom: minZoom,
opacity:0,
width: 6,
zIndex:1,
})
}),
new ol.style.Style({
stroke: new ol.style.Stroke({
color: lightStroke,
//width: 5,
width: 2,
lineDash: [4,8],
lineDashOffset: 6
//zIndex:5
})
}),
new ol.style.Style({
stroke: new ol.style.Stroke({
color: darkStroke,
//width: 5,
width: 5,
lineDash: [4,8],
//lineDashOffset: 6,
//zIndex:1
})
}),
];
return retStyle;
};
var vectorLayer = new ol.layer.VectorTile({
source: new ol.source.VectorTile({
format: new ol.format.MVT(),
url: 'http://localhost:8080/roads/{z}/{x}/{y}.mvt'
}),
style:styleRoadFunction,
declutter: true
})
var map = new ol.Map({
target: 'map',
layers: [osmLayer,vectorLayer],
view: new ol.View({
center: ol.proj.fromLonLat([103.8198,1.3521]),
zoom: 14
})
});
【问题讨论】:
-
样式函数采用特征和分辨率
var styleRoadFunction = function(feature, reolution) {,因此您可以根据分辨率和特征类型调整输出 -
道路因层而异。根据您的层进行particulsr缩放lavel显示。follow the url gis.stackexchange.com/questions/24987/…
-
@Mike 感谢您的回复。我尝试添加 var styleRoadFunction = function(feature, resolution) 并且还定义了一个 var zoom =14 并在每个 if-else 语句中使用不同的 zoom 值分配该缩放。但它不会改变任何东西。我在这里错过了什么吗?
标签: javascript html zooming openlayers styling