问题
I have drawing polylines as the following:
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);
}
I want to place a marker at the start of the polygon line and end of the polygon line. How to do that?
回答1:
- 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 });
回答2:
Since you hold coordinates of polyline in LineCordinates, you can use its first and last elements as a reference for your markers:
...
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"
});
...
来源:https://stackoverflow.com/questions/23198372/polylines-with-start-and-end-of-location-in-google-maps-v3