高德地图 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="info" id="text">
    请用鼠标在地图上操作试试
</div>
<div class="input-card" style="width:16rem">
    <h4>地图点击相关事件</h4>
    <div>
      <div class="input-item">
        <button id="clickOn" class="btn" style="margin-right:1rem;">绑定事件</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
    });



    function showInfoClick(e){
        var text = '您在 [ '+e.lnglat.getLng()+','+e.lnglat.getLat()+' ] 的位置单击了地图!'
        document.querySelector("#text").innerText = text;
    }
    function showInfoDbClick(e){
        var text = '您在 [ '+e.lnglat.getLng()+','+e.lnglat.getLat()+' ] 的位置双击了地图!'
        document.querySelector("#text").innerText = text;
    }
    function showInfoMove(){
        var text = '您移动了您的鼠标!'
        document.querySelector("#text").innerText = text;
    }
    // 事件绑定
    function clickOn(){
        log.success("绑定事件!");  
        map.on('click', showInfoClick);
        map.on('dblclick', showInfoDbClick);
        map.on('mousemove', showInfoMove);
    }
    // 解绑事件
    function clickOff(){
        log.success("解除事件绑定!"); 
        map.off('click', showInfoClick);
        map.off('dblclick', showInfoDbClick);
        map.off('mousemove', showInfoMove);
    }
    
    // 给按钮绑定事件
    document.getElementById("clickOn").onclick = clickOn;
    document.getElementById("clickOff").onclick = clickOff;

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