这里给大家分享我在OpenLayers 地图开发工作中总结出的一下代码和注意点,希望对大家有所帮助

效果如下:


核心代码展示:附带讲解注释
var map = new ol.Map({
target: 'map',
layers: [
new ol.layer.Tile({
source: new ol.source.XYZ({
url:
'http://map.geoq.cn/ArcGIS/rest/services/ChinaOnlineStreetPurplishBlue/MapServer/tile/{z}/{y}/{x}',
wrapX: false
})
}),
new ol.layer.Tile({
source: new ol.source.XYZ({
url:
'http://t{0-7}.tianditu.com/DataServer?T=cia_w&x={x}&y={y}&l={z}&tk=5d27dc75ca0c3bdf34f657ffe1e9881d',
wrapX: false
})
})
],
view: new ol.View({
projection: 'EPSG:4326',
center: [113.87, 22.691],
zoom: 12
}),
});
const BGLayer = new ol.layer.Tile({
source: new ol.source.XYZ({
url: 'http://t{0-7}.tianditu.com/DataServer?T=img_w&x={x}&y={y}&l={z}&tk=5d27dc75ca0c3bdf34f657ffe1e9881d',
wrapX: false
})
})
BGLayer.setVisible(false)
map.addLayer(BGLayer)
var iconFeature = new ol.Feature();
iconFeature.setStyle(createLabelStyle(iconFeature));
var vectorSource = new ol.source.Vector({
features: [iconFeature]
});
var vectorLayer = new ol.layer.Vector({
source: vectorSource
});
map.addLayer(vectorLayer);
function createLabelStyle(feature) {
return new ol.style.Style({
image: new ol.style.Icon({
anchor: [40, 42],
scale: 0.5,
anchorOrigin: 'top-right',
anchorXUnits: 'pixels',
anchorYUnits: 'pixels',
offsetOrigin: 'bottom-left',
opacity: 1,
src: 'dian.png'
}),
text: new ol.style.Text({
textAlign: 'center',
textBaseline: 'middle',
font: 'normal 14px 微软雅黑',
fill: new ol.style.Fill({
color: '#000'
}),
stroke: new ol.style.Stroke({
color: '#F00',
width: 2
})
})
});
}
var newFeature = new ol.Feature({
geometry: new ol.geom.Point([0,0])
});
newFeature.setStyle(createLabelStyle(newFeature));
vectorSource.addFeature(newFeature);
map.on('click', function (evt) {
var coordinate = evt.coordinate;
newFeature.set('geometry',new ol.geom.Point(coordinate))
document.getElementsByClassName("list-box")[0].innerHTML = '<p> 经度:' + coordinate[0].toFixed(3) + ' 纬度:' + coordinate[1].toFixed(3) + '</p>'
});
map.on('moveend', function (evt) {
if (map.getView().getZoom() >= 16) {
BGLayer.setVisible(true)
} else {
BGLayer.setVisible(false)
}
})
点位图片:

完整demo代码:
<!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>点击出点位</title>
<style>
*{padding: 0;margin: 0}
.list-box {
width: 300px;
height: 100px;
background: white;
box-sizing: border-box;
padding: 20px;
line-height: 60px;
overflow: auto;
position: fixed;
right: 10px;
top: 10px;
z-index: 999;
text-align: center;
}
</style>
</head>
<body>
<div id="map"></div>
<div class="list-box">
</div>
<script>
var map = new ol.Map({
target: 'map',
layers: [
new ol.layer.Tile({
source: new ol.source.XYZ({
url:
'http://map.geoq.cn/ArcGIS/rest/services/ChinaOnlineStreetPurplishBlue/MapServer/tile/{z}/{y}/{x}',
wrapX: false
})
}),
new ol.layer.Tile({
source: new ol.source.XYZ({
url:
'http://t{0-7}.tianditu.com/DataServer?T=cia_w&x={x}&y={y}&l={z}&tk=5d27dc75ca0c3bdf34f657ffe1e9881d',
wrapX: false
})
})
],
view: new ol.View({
projection: 'EPSG:4326',
center: [113.87, 22.691],
zoom: 12
}),
});
const BGLayer = new ol.layer.Tile({
source: new ol.source.XYZ({
url: 'http://t{0-7}.tianditu.com/DataServer?T=img_w&x={x}&y={y}&l={z}&tk=5d27dc75ca0c3bdf34f657ffe1e9881d',
wrapX: false
})
})
BGLayer.setVisible(false)
map.addLayer(BGLayer)
var iconFeature = new ol.Feature();
iconFeature.setStyle(createLabelStyle(iconFeature));
var vectorSource = new ol.source.Vector({
features: [iconFeature]
});
var vectorLayer = new ol.layer.Vector({
source: vectorSource
});
map.addLayer(vectorLayer);
function createLabelStyle(feature) {
return new ol.style.Style({
image: new ol.style.Icon({
anchor: [40, 42],
scale: 0.5,
anchorOrigin: 'top-right',
anchorXUnits: 'pixels',
anchorYUnits: 'pixels',
offsetOrigin: 'bottom-left',
opacity: 1,
src: 'dian.png'
}),
text: new ol.style.Text({
textAlign: 'center',
textBaseline: 'middle',
font: 'normal 14px 微软雅黑',
fill: new ol.style.Fill({
color: '#000'
}),
stroke: new ol.style.Stroke({
color: '#F00',
width: 2
})
})
});
}
var newFeature = new ol.Feature({
geometry: new ol.geom.Point([0,0])
});
newFeature.setStyle(createLabelStyle(newFeature));
vectorSource.addFeature(newFeature);
map.on('click', function (evt) {
var coordinate = evt.coordinate;
newFeature.set('geometry',new ol.geom.Point(coordinate))
document.getElementsByClassName("list-box")[0].innerHTML = '<p> 经度:' + coordinate[0].toFixed(3) + ' 纬度:' + coordinate[1].toFixed(3) + '</p>'
});
map.on('moveend', function (evt) {
if (map.getView().getZoom() >= 16) {
BGLayer.setVisible(true)
} else {
BGLayer.setVisible(false)
}
})
</script>
</body>
</html>
如果对您有所帮助,欢迎您点个关注,我会定时更新技术文档,大家一起讨论学习,一起进步。
