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 はパスの # 要素です)。

これがなぜなのか、そしてこれを防ぐことができるかどうかは誰でも知っています(カウンターをメモリに保持することは別として)。

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 に答える