高德地图 JS API示例-地图属性- › 设置/获取地图显示范围

高德地图 JS API示例-地图属性- › 设置/获取地图显示范围

<!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>
0 0 投票数
文章评分
订阅评论
提醒
0 评论
内联反馈
查看所有评论
0
希望看到您的想法,请您发表评论x