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)。
任何人都知道为什么会这样,以及是否可以阻止这种情况(协助将记忆保留在记忆中)。
答案 0 :(得分:0)
您在这里所做的是每次路径更改时都要在路径中添加新的侦听器。你想要做的只是添加一次听众。我认为这应该可以解决问题:
poly = new google.maps.Polyline(polyOptions);
poly.setMap(map);
var path = poly.getPath();
google.maps.event.addListener(map, 'click', addLatLng);
function addLatLng(event) {
// 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
});