画多边形区域简单示例

此示例展示如何在地图区绘制多边形区域。

关键代码

    var bermudaTriangle;
    var triangleCoords = [
        new sogou.maps.LatLng(39.992792,116.326142),
        new sogou.maps.LatLng(39.986017,116.31049),
        new sogou.maps.Point(12949589,4835996),
		new sogou.maps.LatLng(39.992792,116.326142)
    ];
    // 创建多边形
    bermudaTriangle = new sogou.maps.Polygon({
      paths: triangleCoords,
      strokeColor: "#FF0000",
      strokeOpacity: 0.8,
      strokeWeight: 2,
      fillColor: "#FF0000",
      fillOpacity: 0.35
    });
   bermudaTriangle.setMap(map);

代码解析

设置多边形区域的节点坐标。

    var triangleCoords = [
        new sogou.maps.LatLng(39.992792,116.326142),
        new sogou.maps.LatLng(39.986017,116.31049),
        new sogou.maps.Point(12949589,4835996),
		new sogou.maps.LatLng(39.992792,116.326142)
    ];

在地图区创建多边形区域。

    // 创建多边形
    bermudaTriangle = new sogou.maps.Polygon({
      paths: triangleCoords,
      strokeColor: "#FF0000",
      strokeOpacity: 0.8,
      strokeWeight: 2,
      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">
 /**
  * 画多边形区域简单示例
  */
  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);

    var triangleCoords = [
        new sogou.maps.LatLng(39.992792,116.326142),
        new sogou.maps.LatLng(39.986017,116.31049),
        new sogou.maps.Point(12949589,4835996),
		new sogou.maps.LatLng(39.992792,116.326142)
    ];

    // 创建多边形
    bermudaTriangle = new sogou.maps.Polygon({
      paths: triangleCoords,
      strokeColor: "#FF0000",
      strokeOpacity: 0.8,
      strokeWeight: 2,
      fillColor: "#FF0000",
      fillOpacity: 0.35
    });

   bermudaTriangle.setMap(map);
  }
</script>
</head>
<body onload="initialize()">
  <div id="map_canvas"></div>
</body>
</html>

运行代码