删除多个标记示例

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

关键代码

function removeMarkers()
{
	//从地图上删除多个Marker
	for(var i=0;i < markers.length;i++)
	{
		markers[i].setMap(null)
	}
}

代码解析

创建多个地图区标记,并保存到数组中。

    var marker = new sogou.maps.Marker({
        position: myLatlng, 
        map: map,
        title:"Hello World!"
    });
	//存放到数组
	markers.push(marker);

	var marker2 = new sogou.maps.Marker({
        position: new sogou.maps.LatLng(39.981320,116.325247), 
        map: map,
        title:"Hello World!"
    });
	//存放到数组
	markers.push(marker2);
	var marker3 = new sogou.maps.Marker({
        position: new sogou.maps.LatLng(39.980320,116.325247), 
        map: map,
		title:"我不会被删除",
		label:{visible:true}
    });
	//以下这句被注释,没存放到数组,所以这个Marker不会被删除
	//markers.push(marker3);
    

删除地图区多个标记。

  function removeMarkers()
  {
    //从地图上删除多个Marker,未加到数组的不会被删除
	for(var i=0;i<markers.length;i++)
	{
		markers[i].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 markers=[];
  function initialize() {
    var myLatlng = new sogou.maps.LatLng(39.981320,116.320247);
    var myOptions = {
      zoom: 15,
      center: myLatlng,
      mapTypeId: sogou.maps.MapTypeId.ROADMAP
    }
    map = new sogou.maps.Map(document.getElementById("map_canvas"), myOptions);
    
    var marker = new sogou.maps.Marker({
        position: myLatlng, 
        map: map,
        title:"Hello World!"
    });
	//存放到数组
	markers.push(marker);

	var marker2 = new sogou.maps.Marker({
        position: new sogou.maps.LatLng(39.981320,116.325247), 
        map: map,
        title:"Hello World!"
    });
	//存放到数组
	markers.push(marker2);
	var marker3 = new sogou.maps.Marker({
        position: new sogou.maps.LatLng(39.980320,116.325247), 
        map: map,
		title:"我不会被删除",
		label:{visible:true}
    });
	//以下这句被注释,没存放到数组,所以这个Marker不会被删除
	//markers.push(marker3);
    
    var flightPlanCoordinates = [ 
        new sogou.maps.LatLng(39.992792,116.326142), 
        new sogou.maps.Point(12949589,4835996), 
        new sogou.maps.LatLng(39.986017,116.31049) 
    ]; 
    var flightPath = new sogou.maps.Polyline({ 
      path: flightPlanCoordinates, 
      strokeColor: "#FF0000", 
      strokeOpacity: 1.0, 
      strokeWeight: 5,
	  map:map
    }); 
  }
  function removeMarkers()
  {
    //从地图上删除多个Marker,未加到数组的不会被删除
	for(var i=0;i<markers.length;i++)
	{
		markers[i].setMap(null)
	}
  }
</script>
</head>
<body onload="initialize()">
  <input type="button" onclick="removeMarkers()" value="从地图上删除多个标记" />
  <div id="map_canvas"></div>
</body>
</html>

运行代码