高德地图 marker拖拽(一)

这个示例展示了如何在高德地图API中实现覆盖物(Marker)的拖拽事件绑定和解绑。用户可以通过点击'绑定事件'按钮开启标记拖拽功能,并在拖拽过程中触发显示信息的方法。点击'解绑事件'则会取消拖拽功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<!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 {
      height: 100%;
      width: 100%;
    }
  </style>
</head>

<body>
  <div id="container"></div>

  <div class="input-card" style="width:18rem">
    <h4>覆盖物拖拽事件的绑定与解绑</h4>
    <div>
      <div class="input-item">
        <button id="clickOn" class="btn" style="margin-right:1rem;" onclick="clickOn()">绑定事件</button>
        <button id="clickOff" class="btn">解绑事件</button>
      </div>
    </div>
  </div>
  <script type="text/javascript" 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 type="text/javascript">
    //初始化地图对象,加载地图
    var map = new AMap.Map("container", {
      resizeEnable: true
    });
    var marker = new AMap.Marker({
      map: map,
      icon: "https://webapi.amap.com/theme/v1.3/markers/n/mark_b.png",
      position: [116.405467, 39.907761],
      draggable: false
    });

    map.setFitView();

    function clickOn () {
      alert('绑定事件')
      marker.setDraggable(true)
      marker.on('dragging', showInfoM);
    }

    function showInfoM (e) {
      console.log(e)
    }



  </script>
</body>

</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值