标记可拖拽示例

此示例展示如何创建一个可拖动标记。

关键代码

    var marker = new sogou.maps.Marker({
        position: myLatlng, 
        map: map,
		//设置标记可被拖动
		draggable:true,
        styleId:"S1897"
    });
	sogou.maps.event.addListener(marker,"dragend",function(mouseEvent){
		//参数返回mouseEvent对象
		//拖动结束位置的坐标为:mouseEvent.point
		//其他事件请查看文档说明
		alert("拖动结束位置的坐标为:"+mouseEvent.point);
	});

代码解析

创建一个地图区标记,设置参数draggable为true,则标记可以被拖动。

    var marker = new sogou.maps.Marker({
        position: myLatlng, 
        map: map,
		//设置标记可被拖动
		draggable:true,
        styleId:"S1897"
    });	

添加拖动结束时的侦听。弹出拖动结束时的坐标位置。

sogou.maps.event.addListener(marker,"dragend",function(mouseEvent){
		//参数返回mouseEvent对象
		//拖动结束位置的坐标为:mouseEvent.point
		//其他事件请查看文档说明
		alert("拖动结束位置的坐标为:"+mouseEvent.point);
	});

完整代码

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<title>搜狗地图 JavaScript API 示例: 标记可拖拽示例</title>
<style type="text/css">
html {height: auto;}
body {height: auto;margin: 0;padding: 0;}
#map_canvas {width:1000px;height: 500px;position: absolute;}
@media print {#map_canvas {height: 950px;}}
</style>
<script type="text/javascript" src="//api.go2map.com/maps/js/api_v2.5.1.js"></script>
<script type="text/javascript">
  function initialize() {
    var myLatlng = new sogou.maps.LatLng(39.981320,116.320247);
    var myOptions = {
      zoom: 15,
      center: myLatlng,
      mapTypeId: sogou.maps.MapTypeId.ROADMAP
    }
    var map = new sogou.maps.Map(document.getElementById("map_canvas"), myOptions);
    
    var marker = new sogou.maps.Marker({
        position: myLatlng, 
        map: map,
		//设置标记可被拖动
		draggable:true,
        styleId:"S1897"
    });
	sogou.maps.event.addListener(marker,"dragend",function(mouseEvent){
		//参数返回mouseEvent对象
		//拖动结束位置的坐标为:mouseEvent.point
		//其他事件请查看文档说明
		alert("拖动结束位置的坐标为:"+mouseEvent.point);
	});
  }
</script>
</head>
<body onload="initialize()">
  <div id="map_canvas"></div>
</body>
</html>

运行代码