删除所有覆盖物示例

此示例展示如何删除地图区所有覆盖物。点击按键将删除所有覆盖物。

关键代码

  function clearMarker()
  {
    //从地图上删除Marker、Polyline等所有覆盖物
	map.clearAll();
  }

代码解析

添加覆盖物。

   var marker = new sogou.maps.Marker({
        position: myLatlng, 
        map: map,
        title:"Hello World!"
    });
	var marker2 = new sogou.maps.Marker({
        position: new sogou.maps.LatLng(39.981320,116.325247), 
        map: map,
        title:"Hello World!"
    });
	var marker3 = new sogou.maps.Marker({
        position: new sogou.maps.LatLng(39.980320,116.325247), 
        map: map,
		title:"isFixed=true了,所以我不会被删除",
		label:{visible:true},
		isFixed:true	//isFixed为true,则次Marker不会被删除
    });
    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 clearMarker()
  {
    //从地图上删除Marker、Polyline等所有不被fix的覆盖物
	map.clearAll();
  }

完整代码

<!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 map;
  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!"
    });
	var marker2 = new sogou.maps.Marker({
        position: new sogou.maps.LatLng(39.981320,116.325247), 
        map: map,
        title:"Hello World!"
    });
	var marker3 = new sogou.maps.Marker({
        position: new sogou.maps.LatLng(39.980320,116.325247), 
        map: map,
		title:"isFixed=true了,所以我不会被删除",
		label:{visible:true},
		isFixed:true	//isFixed为true,则次Marker不会被删除
    });
    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 clearMarker()
  {
    //从地图上删除Marker、Polyline等所有不被fix的覆盖物
	map.clearAll();
  }
</script>
</head>
<body onload="initialize()">
  <input type="button" onclick="clearMarker()" value="从地图上删除所有覆盖物" />
  <div id="map_canvas"></div>
</body>
</html>

运行代码