谷歌地图api v3 - 地图工具栏实现 - 用于dblclick缩放的settimeout

时间:2014-01-22 22:16:38

标签: javascript google-maps-api-3 settimeout

我使用地图工具栏实现javascript Google maps api v3: http://nettique.free.fr/gmap/toolbar.html

工作得很好!我唯一的问题是我想在创建多边形时保持dblclick事件的缩放,但现在,如果我双击,我会创建一个新标记并立即将其删除。

使用旧版本的代码,我正在使用如下所述的超时: https://stackoverflow.com/a/8417447/1895428

但是现在,无论我把setTimeout函数放在哪里,它都没有变化。有人知道放在哪里吗?

我尝试修改js代码(http://nettique.free.fr/gmap/lib/mapToolbar.js)中的addPoint函数,但它不起作用:

addPoint : function(e, poly, index) { //alert(MapToolbar["shapeCounter"]);
        update_timeout = setTimeout(function(){
        var e = (typeof e.latLng != "undefined")? e.latLng : e,
            image = new google.maps.MarkerImage('/image/marker-edition.png',
            new google.maps.Size(9, 9),
            new google.maps.Point(0, 0),
            new google.maps.Point(5, 5)),
        imageover = new google.maps.MarkerImage('/image/marker-edition-over.png',
            new google.maps.Size(9, 9),
            new google.maps.Point(0, 0),
            new google.maps.Point(5, 5)),
                path = poly.getPath(),
                index = (typeof index != "undefined")? index : path.length,
                markers = (poly.markers)? poly.markers : new google.maps.MVCArray, 
        marker = new google.maps.Marker({
                    position: e,
                    map: map[mapkey],
                    draggable: true,
                    icon: image
        });

        marker.index = index;    
        path.insertAt(index, e);
        markers.insertAt(index, marker)
        if(arguments[2]){
            MapToolbar.reindex(markers);    
        } }, 200);

//click on a polymarker will delete it

        google.maps.event.addListener(marker, 'click', function() {
            marker.setMap(null);
            markers.removeAt(marker.index);
            path.removeAt(marker.index);
            MapToolbar.reindex(markers);                
            if(markers.getLength() == 0){
                MapToolbar.removeFeature(poly.id);
            }
        });

/*
      google.maps.event.addListener(marker, 'dragstart', function() {
                MapToolbar.currentlyDragging = true;
        })
*/      
      google.maps.event.addListener(marker, 'position_changed', function() {
              path.setAt(marker.index, marker.getPosition());
        })

        google.maps.event.addListener(marker, 'dragend', function() {
                //MapToolbar.currentlyDragging = false;
            path.setAt(marker.index, marker.getPosition());
            var position = marker.getPosition(),
                    p;

//get previous point

            if(typeof path.getAt(marker.index-1) != "undefined"){
                var m1 = path.getAt(marker.index -1);
                        p = MapToolbar.getMidPoint(position, m1);       
                        MapToolbar.addPoint(p, poly, marker.index);                     
            }

// get next point

            if(typeof path.getAt(marker.index+1) != "undefined"){
                var m2 = path.getAt(marker.index+1);
                        p = MapToolbar.getMidPoint(position, m2);       
                        MapToolbar.addPoint(p, poly, marker.index+1);                       
            }           
        });

        google.maps.event.addListener(marker, 'mouseover', function() {
                this.setIcon(imageover);
        });   

        google.maps.event.addListener(marker, 'mouseout', function() {
                this.setIcon(image);
        }); 

        MapToolbar["isStarted"]++;
    }

1 个答案:

答案 0 :(得分:0)

不确定这是否有效,但是在stackoverflow示例之后,您只需更改侦听器代码:

addPoint : function(e, poly, index) {
    var e = (typeof e.latLng != "undefined")? e.latLng : e,
    ...

//click on a polymarker will delete it

    var update_timeout = null;

    google.maps.event.addListener(marker, 'click', function() {
        update_timeout = setTimeout(function() {
            marker.setMap(null);
            markers.removeAt(marker.index);
            path.removeAt(marker.index);
            MapToolbar.reindex(markers);                
            if(markers.getLength() == 0){
                MapToolbar.removeFeature(poly.id);
            }
        }, 200);
    });

    google.maps.event.addListener(marker, 'dblclick', function(event) {       
        clearTimeout(update_timeout);
    });
    ...