Search code examples
javascriptgoogle-maps-api-3google-polyline

How to get polyline point on click on this point?


I have a polyline, and want to delete some points on right click. So is there a way to get the poly point on rightclick? i can get the coords of the map and compare them with poly array of dots, but i thisn there must be a simple way.

enter image description here

thanks


Solution

  • You can get the coordinates of a point on the line when you right-click it:

    polyline.addListener('rightclick', function(polyMouseEvent) {
        var coords = polyMouseEvent.latLng;
    });
    

    It's a bit trickier to then determine if that point is one of the nodes. Although according to the docs you can find out "The index of the vertex beneath the cursor when the event occurred, if the event occurred on a vertex and the polyline or polygon is editable". I seem to recall not having much luck with this though.

    And I think you need to delete a section of the path, not just remove a single coordinate from it. Unless perhaps you completely redraw the polyline each time.

    I did something vaguely similar here, where I delete sections of the path whenever you right-click on that bit of the polyline.