poly = new google.maps.Polyline(polyOptions);
poly.setMap(map);
google.maps.event.addListener(map, 'click', addLatLng);
function addLatLng(event) {
var path = poly.getPath();
// Because path is an MVCArray, we can simply append a new coordinate
// and it will automatically appear
path.push(event.latLng);
// Add a new marker at the new plotted point on the polyline.
var marker = new google.maps.Marker({
position: event.latLng,
title: '#' + path.getLength(),
map: map,
dragable: false,
clickable: true,
name: name,
raiseOnDrag: false,
});
//This event is fired when setAt() is called. The event passes the index that was passed to setAt() and the element that was previously in the array at that index.
google.maps.event.addListener(path, 'set_at', function(index,oldWP) {
//called when editing the path
//we need to remove the (current) marker and add a new one at the position
});
google.maps.event.addListener(path, 'insert_at', function(index) {
//when insert happens not at the end of the path but somewhere in the middle
//We need to completely rerender all makers
});
}
Google 文档:调用 setAt() 时会触发此事件。该事件传递传递给 setAt() 的索引和该索引处先前在数组中的元素。
但是,此事件被称为 x 次(其中 x 是路径的 # elem)。
任何人都知道这是为什么以及是否可以防止这种情况(除了在内存中保留一个计数器)。