Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Map Editable Polygon Filter Drag event from set_at events

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.

like image 407
James Lin Avatar asked Oct 31 '13 18:10

James Lin


2 Answers

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

like image 197
Dr.Molle Avatar answered Nov 18 '22 16:11

Dr.Molle


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
        });
    });
like image 39
Kobold_Warlord Avatar answered Nov 18 '22 16:11

Kobold_Warlord