admin管理员组

文章数量:1398831

I have a editable polygon and I want to listen to events when a vertex is dragged(polygon resized) . Normally attaching the paths to 'set_at' event is good but it fires a lot of events when the whole polygon is dragged.

google.maps.event.addListener(polygon, 'dragend', function(){search();});
google.maps.event.addListener(polygon.getPath(), 'insert_at', function(e, e1){search();});
google.maps.event.addListener(polygon.getPath(), 'remove_at', function(e, e1){search();});
//this also fires a lot of events when ploygon is dragged
google.maps.event.addListener(polygon.getPath(), 'set_at', function(){search();});

What I want to achieve is have an event something like "shape_changed" which doesn't fire events when it is dragged.

I have a editable polygon and I want to listen to events when a vertex is dragged(polygon resized) . Normally attaching the paths to 'set_at' event is good but it fires a lot of events when the whole polygon is dragged.

google.maps.event.addListener(polygon, 'dragend', function(){search();});
google.maps.event.addListener(polygon.getPath(), 'insert_at', function(e, e1){search();});
google.maps.event.addListener(polygon.getPath(), 'remove_at', function(e, e1){search();});
//this also fires a lot of events when ploygon is dragged
google.maps.event.addListener(polygon.getPath(), 'set_at', function(){search();});

What I want to achieve is have an event something like "shape_changed" which doesn't fire events when it is dragged.

Share Improve this question edited Oct 31, 2013 at 19:46 JJJ 33.2k20 gold badges94 silver badges103 bronze badges asked Oct 31, 2013 at 18:52 James LinJames Lin 26.6k39 gold badges144 silver badges249 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

Remove the set_at-listener on dragstart and re-assign the set_at-listener on dragend

Another option is to set a flag at dragstart and at dragend, and have your set_at listeners look at that flag before doing anything:

    polygon.addListener('dragstart', function (event) {
        dragging = true;
    });

    polygon.addListener('dragend', function (event) {
        //do drag end stuff here
        dragging = false;
    });

    //setup resize handler  
    var paths = polygon.getPaths();
    paths.forEach(function (path) {
        path.addListener('set_at', function (event) {
            if (!dragging) //ignore this event while dragging
                //do resize stuff here
        });
    });

本文标签: javascriptGoogle Map Editable Polygon Filter Drag event from setat eventsStack Overflow