简单删除标记示例

此示例展示如何删除地图区标记。点击按键将删除标记。

关键代码

  function removeMarker()
  {
    //从地图上删除标记
	marker.setMap(null)
  }

代码解析

创建一个地图区标记。

    marker = new sogou.maps.Marker({
        position: myLatlng, 
        map: map,
        title:"Hello World!"
    });	

删除地图区标记。

  function removeMarker()
  {
    //从地图上删除标记
	marker.setMap(null)
  }

完整代码

<!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">
  var marker;
  function initialize() {
    var myLatlng = new sogou.maps.LatLng(39.981320,116.320247);
    var myOptions = {
      zoom: 3,
      center: myLatlng,
      mapTypeId: sogou.maps.MapTypeId.ROADMAP
    }
    var map = new sogou.maps.Map(document.getElementById("map_canvas"), myOptions);
    
    marker = new sogou.maps.Marker({
        position: myLatlng, 
        map: map,
        title:"Hello World!"
    });	   
  }
  function removeMarker()
  {
    //从地图上删除标记
	marker.setMap(null)
  }
</script>
</head>
<body onload="initialize()">
  <input type="button" onclick="removeMarker()" value="从地图上删除标记" />
  <div id="map_canvas"></div>
</body>
</html>

运行代码