画折线简单示例

此示例展示如何在地图区绘制折线。

关键代码

	//坐标可以是经纬度坐标,也可以是搜狗坐标
    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
    });
   flightPath.setMap(map);

代码解析

设置折线对象的坐标。

	//坐标可以是经纬度坐标,也可以是搜狗坐标
    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
    });
   flightPath.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.988176,116.319404);
    var myOptions = {
      zoom: 15,
      center: myLatLng,
      mapTypeId: sogou.maps.MapTypeId.ROADMAP
    };

    var map = new sogou.maps.Map(document.getElementById("map_canvas"), myOptions);

	//坐标可以是经纬度坐标,也可以是搜狗坐标
    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
    });

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

运行代码