Google地图v3中包含位置开始和结束的折线

时间:2014-04-21 13:25:42

标签: google-maps google-maps-api-3

我绘制折线如下:

var data = JSON.parse(data);
     var LineCordinates = new Array();
     for (i=0; i<data.length; i++){
     LineCordinates[i] = new google.maps.LatLng(data[i].fields.latitude, data[i].fields.longitude);
         }
      linePath = new google.maps.Polyline({
        path: LineCordinates,
        geodesic: true,
        strokeColor: '#FF0000',
        strokeOpacity: 1.0,
        strokeWeight: 2
      });

      linePath.setMap(map);

    }

我想在多边形线的markerpolygon line的开头放置一个end。怎么做?

2 个答案:

答案 0 :(得分:3)

  • documentation on google.maps.Polyline
  • markers

    var startMarker = new google.maps.Marker({
          position:linepath.getPath().getAt(0), 
          map:map
        });
    var endMarker =  new google.maps.Marker({
          position:linepath.getPath().getAt(linepath.getPath().getLength()-1), 
          map:map
        });
    

答案 1 :(得分:0)

由于您在LineCordinates中保持折线的坐标,因此您可以使用其第一个和最后一个元素作为标记的参考:

...
linePath.setMap(map);

var marker1 = new google.maps.Marker({
   position: LineCordinates[0],
   map: map,
   title: "Start"
});

var marker2 = new google.maps.Marker({
   position: LineCordinates[ LineCordinates.length - 1 ],
   map: map,
   title: "End"
});
...