在Google Maps V3 API中多次调用set_at [英] set_at called multiple times in Google Maps V3 API

查看:121
本文介绍了在Google Maps V3 API中多次调用set_at的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  poly = new google.maps.Polyline(polyOptions); 
poly.setMap(map);

google.maps.event.addListener(地图,'click',addLatLng);

函数addLatLng(event){

var path = poly.getPath();

//因为path是一个MVCArray,我们可以简单地追加一个新的坐标
//并且它会自动出现
path.push(event.latLng);

//在折线上的新绘制点上添加一个新标记。
var marker = new google.maps.Marker({
position:event.latLng,
title:'#'+ path.getLength(),$ b $ map:map,
dragable:false,
clickable:true,
name:name,
raiseOnDrag:false,
});

//当调用setAt()时触发此事件。该事件传递传递给setAt()的索引以及之前位于该索引处数组中的元素。
google.maps.event.addListener(path,'set_at',function(index,oldWP){
//编辑路径时调用
//我们需要移除(current)标记并在位置

})添加一个新的标记;

google.maps.event.addListener(path,'insert_at',function(index){
//当插入不在路径的末尾,而是在中间的某处时
//我们需要完全放弃所有制造商

}); Google文档:当setAt()被调用时,该事件被触发。该事件传递传递给setAt()的索引以及之前位于该索引数组中的元素。



然而,这个事件被称为x次数(其中x是路径的#elem)。

有人知道这是为什么,并且这是可以防止的(帮助保留一个计数器在内存中) / p>

解决方案

您在这里所做的是您每次路径更改时都将新的侦听器添加到路径中。你想要做的只是添加一次监听器。我认为这应该可以做到这一点:$ b​​
$ b pre $ poly $ google =
poly.setMap(map);
var path = poly.getPath();

google.maps.event.addListener(地图,'click',addLatLng);

函数addLatLng(event){

//因为path是一个MVCArray,我们可以简单地追加一个新的坐标
//并且它会自动出现
path.push(event.latLng);

//在折线上的新绘制点上添加一个新标记。
var marker = new google.maps.Marker({
position:event.latLng,
title:'#'+ path.getLength(),$ b $ map:map,
dragable:false,
clickable:true,
name:name,
raiseOnDrag:false,
});
}

//当setAt()被调用时,这个事件被触发。该事件传递传递给setAt()的索引以及之前位于该索引处数组中的元素。
google.maps.event.addListener(path,'set_at',function(index,oldWP){
//编辑路径时调用
//我们需要移除(current)标记并在位置

})添加一个新的标记;

google.maps.event.addListener(path,'insert_at',function(index){
//当插入不在路径的末尾,而是在中间的某处时
//我们需要完全放弃所有制造商

});


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 documentation: 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.

However this event is called x times (where x is the # elem of the path).

Anybody know why this is and if this can be prevented (asside from keeping a counter in memory).

解决方案

What you are doing here is that you are adding new listeners to the path for each time the path changes. What you want to do is to only add the listeners once. I think this should do the trick:

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 

});

这篇关于在Google Maps V3 API中多次调用set_at的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆