0
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)。

任何人都知道这是为什么以及是否可以防止这种情况(除了在内存中保留一个计数器)。

4

1 回答 1

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 

});
于 2013-04-05T11:53:35.773 回答