feature樣式image會存在src資源找不到問題,故而只寫了text以查看效果
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://openlayers.org/en/v5.3.0/css/ol.css" />
<script type="text/javascript" src="https://openlayers.org/en/v5.3.0/build/ol.js"></script>
<title>Document</title>
</head>
<body>
<div id="map"></div>
<script>
var beijing = ol.proj.fromLonLat([116.28, 39.54]);
var map = new ol.Map({
target: 'map',
layers: [
new ol.layer.Tile({
source: new ol.source.OSM()
})
],
view: new ol.View({
center: beijing,
zoom: 4
})
});
//實例化矢量點要素,通過矢量圖層添加到地圖容器中
//這樣就實現了預先加載圖文標注
var iconFeature = new ol.Feature({
geometry: new ol.geom.Point(beijing),
name: '北京市', //名稱屬性
population: 2115 //人口數(萬)
});
//設置點要素樣式
iconFeature.setStyle(createLabelStyle(iconFeature));
//矢量標注的數據源
var vectorSource = new ol.source.Vector({
features: [iconFeature]
});
//矢量標注圖層
var vectorLayer = new ol.layer.Vector({
source: vectorSource,
visible: false
});
map.addLayer(vectorLayer);
//矢量標注樣式設置函數,設置image為圖標ol.style.Icon
function createLabelStyle(feature){
console.log(feature);
return new ol.style.Style({
text: new ol.style.Text({
textAlign: 'center', //位置
textBaseline: 'bottom', //基准線
font: 'normal 12px 微軟雅黑', //文字樣式
text: feature.get('name'), //文本內容
fill: new ol.style.Fill({ //文本填充樣式(即文字顏色)
color: '#000'
}),
stroke: new ol.style.Stroke({
color: '#F00',
width: 2
})
})
});
}
var flag=true
var animation=setInterval(() => {
flag=!flag
vectorLayer.setVisible(flag)
}, 300);
// 如果需要一段時間后停止閃爍,放開下面的注釋即可
// 1500ms后清楚定時器停止閃爍
// setTimeout(() => {
// clearInterval(animation)
// vectorLayer.setVisible(true)
// }, 1500);
</script>
</body>
</html>
單獨閃爍某些點,可以放單獨圖層(feature樣式圖標src如果找不到可以使用如上text查看效果),如果報錯,看看是不是圖片地址找不到,網絡原因
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://openlayers.org/en/v5.3.0/css/ol.css" />
<script type="text/javascript" src="https://openlayers.org/en/v5.3.0/build/ol.js"></script>
<title>openlayers 標記點閃爍</title>
</head>
<body>
<div id="map"></div>
<script>
var beijing = ol.proj.fromLonLat([116.28, 39.54]);
var map = new ol.Map({
target: 'map',
layers: [
new ol.layer.Tile({
source: new ol.source.OSM()
})
],
view: new ol.View({
center: beijing,
zoom: 4
})
});
//實例化矢量點要素,通過矢量圖層添加到地圖容器中
//這樣就實現了預先加載圖文標注
var iconFeature = new ol.Feature({
geometry: new ol.geom.Point(beijing),
name: '北京市', //名稱屬性
population: 2115 //人口數(萬)
});
var iconFeature1 = new ol.Feature({
geometry: new ol.geom.Point(ol.proj.fromLonLat([115.28, 38.54])),
name: 'point1', //名稱屬性
population: 2115 //人口數(萬)
});
//設置點要素樣式
iconFeature.setStyle(createLabelStyle(iconFeature));
iconFeature1.setStyle(createLabelStyle(iconFeature1));
//矢量標注的數據源
var vectorSource = new ol.source.Vector({
features: [iconFeature]
});
//矢量標注圖層
var vectorLayer = new ol.layer.Vector({
source: vectorSource,
visible: true
});
var vectorLayer1 = new ol.layer.Vector({
source: new ol.source.Vector({
features: [iconFeature1]
}),
visible: false
});
map.addLayer(vectorLayer);
map.addLayer(vectorLayer1);
//矢量標注樣式設置函數,設置image為圖標ol.style.Icon
function createLabelStyle(feature){
console.log(feature);
return new ol.style.Style({
image: new ol.style.Icon({
anchor: [0.5, 60], //錨點
anchorOrigin:'top-right', //錨點源
anchorXUnits: 'fraction', //錨點X值單位
anchorYUnits: 'pixels', //錨點Y值單位
offsetOrigin: 'top-right', //偏移原點
opacity: 0.75,
scale: 0.05,
src: 'https://www.baidu.com/img/flexible/logo/pc/result@2.png' //圖標的URL
}),
});
}
var flag=true
var animation=setInterval(() => {
flag=!flag
vectorLayer1.setVisible(flag)
}, 300);
// 如果需要一段時間后停止閃爍,放開下面的注釋即可
// 1500ms后清楚定時器停止閃爍
// setTimeout(() => {
// clearInterval(animation)
// vectorLayer.setVisible(true)
// }, 1500);
</script>
</body>
</html>