画多边形区域、边自动闭合

此示例展示绘制多边形区域时,最后一个点和第一个点不重合时,多边形自动闭合。

关键代码

	//只给了3个坐标点,最后一个点和第一个点不重合,Polygon对象会自动闭合最后一边
	//支持经纬度坐标和搜狗坐标
    var triangleCoords = [
        new sogou.maps.LatLng(39.992792,116.326142),
        new sogou.maps.LatLng(39.986017,116.31049),
        new sogou.maps.Point(12949589,4835996)
    ];
    bermudaTriangle = new sogou.maps.Polygon({
      path: triangleCoords,
      strokeColor: "#FF0000",
      strokeOpacity: 0.8,
      strokeWeight: 3,
      fillColor: "#FF0000",
      fillOpacity: 0.35
    });
   bermudaTriangle.setMap(map);

代码解析

多边形的节点坐标。最后一个点和第一个点不重合。

	//只给了3个坐标点,最后一个点和第一个点不重合,Polygon对象会自动闭合最后一边
	//支持经纬度坐标和搜狗坐标
    var triangleCoords = [
        new sogou.maps.LatLng(39.992792,116.326142),
        new sogou.maps.LatLng(39.986017,116.31049),
        new sogou.maps.Point(12949589,4835996)
    ];

绘制多边形。自动闭合。

    bermudaTriangle = new sogou.maps.Polygon({
      path: triangleCoords,
      strokeColor: "#FF0000",
      strokeOpacity: 0.8,
      strokeWeight: 3,
      fillColor: "#FF0000",
      fillOpacity: 0.35
    });
   bermudaTriangle.setMap(map);

完整代码

<!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">
 /**
  * 画多边形区域、边自动闭合
  * 注意,只给了3个坐标点,最后一个点和第一个点不重合
  */
  function initialize() {
    var myLatLng = new sogou.maps.LatLng(39.987636,116.320492);
    var myOptions = {
      zoom: 15,
      center: myLatLng,
      mapTypeId: sogou.maps.MapTypeId.ROADMAP
    };
    var bermudaTriangle;
    var map = new sogou.maps.Map(document.getElementById("map_canvas"),
        myOptions);
	//只给了3个坐标点,最后一个点和第一个点不重合,Polygon对象会自动闭合最后一边
	//支持经纬度坐标和搜狗坐标
    var triangleCoords = [
        new sogou.maps.LatLng(39.992792,116.326142),
        new sogou.maps.LatLng(39.986017,116.31049),
        new sogou.maps.Point(12949589,4835996)
    ];
    bermudaTriangle = new sogou.maps.Polygon({
      path: triangleCoords,
      strokeColor: "#FF0000",
      strokeOpacity: 0.8,
      strokeWeight: 3,
      fillColor: "#FF0000",
      fillOpacity: 0.35
    });
   bermudaTriangle.setMap(map);
  }
</script>
</head>
<body onload="initialize()">
  <div id="map_canvas"></div>
</body>
</html>

运行代码