Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Maps V3: drawing library

I love the new Drawing library for Google Maps v3.7, but I have the feeling the documentation is still not complete and I have a couple of questions:

  1. Documentation only mentions the overlaycompleted and {overlay}completed events, but I've also found the drawingmode_changed event. Is there a list of events somewhere?

  2. Is there a way to getting a reference (object) to the polygon which we are drawing besides waiting until we finish drawing it and using a listener with overlaycompleted as is done in the above example?

  3. Is there a way can we delete verteces from a polygon, by simply clicking on them? If not, how would you go about it?

like image 415
Adrien Hingert Avatar asked Nov 19 '11 10:11

Adrien Hingert


2 Answers

You can call any overlay event on complete like this.

google.maps.event.addListener(drawingManager, 'polygoncomplete', function(e) {
    //Your magic goes here 
});

overlaycomplete is like a global event for any overlay, such as markers, polylines, circles and polygons. but if you want to create a listener for an specific overlay, just change overlay to your desired overlay: polygoncomplete, polylinecomplete, circlecomplete.

I know this questions is old, but I wanted to share my answer anyway.

like image 124
David Aguiñaga Avatar answered Oct 17 '22 15:10

David Aguiñaga


Very old question, but I have some update on this, maybe will help somebody.

  1. There are some additional events for editable shapes that could be useful: https://developers.google.com/maps/documentation/javascript/overlays#user_editable_shapes_events

  2. Also couldn't find solution for this.

  3. This piece of code shows how to remove polygon/polyline vertex on left click:

google.maps.event.addListener(drawingManager, 'overlaycomplete', function (e) {
    if (e.type !== google.maps.drawing.OverlayType.MARKER) {
        // Switch back to non-drawing mode after drawing a shape.
        drawingManager.setDrawingMode(null);

        // Add an click event listener for newly-drawn shape
        // and remove polygon vertex if it was clicked
        var newShape = e.overlay;
        google.maps.event.addListener(newShape, 'click', function (e) {
            if (e.vertex !== undefined) {
                if (newShape.type === google.maps.drawing.OverlayType.POLYGON) {
                    var path = newShape.getPaths().getAt(e.path);
                    path.removeAt(e.vertex);
                    if (path.length < 3) {
                        newShape.setMap(null);
                    }
                }
                if (newShape.type === google.maps.drawing.OverlayType.POLYLINE) {
                    var path = newShape.getPath();
                    path.removeAt(e.vertex);
                    if (path.length < 2) {
                        newShape.setMap(null);
                    }
                }
            }
        });
    }
});
like image 39
Rafal Gałka Avatar answered Oct 17 '22 16:10

Rafal Gałka