高德地图 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%;
    }
  </style>
</head>

<body>
  <div id="container"></div>
  <div class="input-card" style="width:24rem;">
    <h4>设置地图当前行政区</h4>
    <div class="input-item">
      <input id='city-name' placeholder="北京市" type="text" style="margin-right:1rem;"><button class="btn" id="query">去指定城市</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,
      center: [116.405285,39.904989]
    });

    //根据cityname、adcode、citycode设置地图位置
    function gotoCity() {
      var val = document.querySelector('#city-name').value; //可以是cityname、adcode、citycode
      if (!val) {
        val = "北京市";
      }
      map.setCity(val);
      log.info(`已跳转至${val}`);
    }

    //绑定查询点击、回车事件
    document.querySelector('#query').onclick = gotoCity;
    document.querySelector('#city-name').onkeydown = function(e) {
      if (e.keyCode === 13) {
        gotoCity();
        return false;
      }
      return true;
    };
  </script>
</body>

</html>
0 0 投票数
文章评分
订阅评论
提醒
0 评论
内联反馈
查看所有评论
0
希望看到您的想法,请您发表评论x