<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<!-- 1、导入openlayers的依赖 -->
<link rel="stylesheet" href="https://lib.baomitu.com/ol3/4.6.5/ol.css">
<script src="https://lib.baomitu.com/ol3/4.6.5/ol.js"></script>
<style>
* {
margin: 0;
padding: 0
}
#map {
width: 100vw;
height: 100vh;
}
.ol-zoomslider {
top: 7.5em !important;
background-color: #652e8080;
}
.ol-zoomslider:hover {
cursor: pointer;
background-color: #652e8090;
}
#mouse-position {
position: fixed;
bottom: 20px;
z-index: 100;
left: 50%;
transform: translateX(-50%);
width: 200px;
height: 40px;
background-color: #652e8080;
text-align: center;
line-height: 40px;
}
</style>
</head>
<body>
<div id="mouse-position">
</div>
<!-- 2、设置地图容器的挂载点 -->
<div id="map">
</div>
<script>
/* 3、设置一个瓦片图层 */
const gaode = new ol.layer.Tile({
title: "高德地图",
source: new ol.source.XYZ({
url: 'http://wprd0{1-4}.is.autonavi.com/appmaptile?lang=zh_cn&size=1&style=7&x={x}&y={y}&z={z}',
wrapX: false
})
});
/* 4、实例化地图 */
/* new Vue({
el
}) */
const map = new ol.Map({
/* 将实例化的地图挂载到对应的DOM元素上 */
target: "map",
layers: [gaode],
/* 设置地图以中心点显示/坐标显示/方法级别显示 */
view: new ol.View({
center: [114.30, 30.50],
zoom: 12,
/* 坐标 */
projection: "EPSG:4326"
})
})
/* 给地图绑定鼠标移动事件,获取坐标 */
map.on("pointermove", evt => {
let mouse = document.getElementById("mouse-position");
mouse.innerHTML = evt.coordinate.map(item => item.toFixed(2));
})
</script>
</body>
</html>
OpenLayer-鼠标位置事件
于 2024-04-27 12:34:28 首次发布