高德地图获取可视范围的经纬度

时间:2024-04-17 22:18:12

参考原文链接:https://lbs.amap.com/api/javascript-api/example/map/map-bounds/

<!doctype html>
<html>

<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width">
<title>地图显示范围</title>
<link rel="stylesheet" href="https://a.amap.com/jsapi_demos/static/demo-center/css/demo-center.css" />
<style>
html,
body,
#container {
width: 100%;
height: 100%;
}

.lnglat {
color: #0288d1;
}
</style>
</head>

<body>
<div id="container"></div>
<div class="info">
<h4>当前地图显示范围(Bounds)</h4>
<p>NorthEast坐标:<span id="ne" class="lnglat"></span></p>
<p>SouthWest坐标:<span id="sw" class="lnglat"></span></p>
</div>
<div class="input-card" style="width:16rem;">
<h4>控制地图显示范围</h4>
<div class="input-item">
<button class="btn" id="reset-bounds">指定地图显示范围</button>
</div>
</div>
<script src="https://webapi.amap.com/maps?v=1.4.15&key=您申请的key值"></script>
<script src="https://a.amap.com/jsapi_demos/static/demo-center/js/demoutils.js"></script>
<script>
//创建地图
var map = new AMap.Map(\'container\', {
resizeEnable: true,
zoom: 10,
center: [116.405285, 39.904989],
showIndoorMap: false
});

//显示当前地图边界范围坐标
function logMapBounds() {
var bounds = map.getBounds();
document.querySelector("#ne").innerText = bounds.northeast.toString();
document.querySelector("#sw").innerText = bounds.southwest.toString();
}

logMapBounds();

//绑定地图移动与缩放事件
map.on(\'moveend\', logMapBounds);
map.on(\'zoomend\', logMapBounds);

//绑定按钮事件
document.querySelector("#reset-bounds").onclick = function() {
//通过 new AMap.Bounds(southWest:LngLat, northEast:LngLat) 或者 map.getBounds() 获得地图Bounds信息
var mybounds = new AMap.Bounds([116.319665, 39.855919], [116.468324,39.9756]);
map.setBounds(mybounds);
}
</script>
</body>

</html>