Polygon verteces events

5,073 views
Skip to first unread message

Adrien

unread,
Nov 20, 2011, 5:49:47 AM11/20/11
to Google Maps JavaScript API v3
I am working with the new drawing library from v3.7 and there's a
couple fo things I'm not sure how I should be doing. Does anybody
know:

1. How to capture the index id of a polygon's vertex when clicking on
a vertex of a polygon? I want to use this to delete verteces by
clicking on them.

2. What event is fired if a polygon has been changed, ie by dragging a
vertex?

Both the above questions refer to polygons which have been set to
"setEditable(true)".

Enoch Lau (Google Employee)

unread,
Nov 20, 2011, 10:01:15 AM11/20/11
to google-map...@googlegroups.com

1. How to capture the index id of a polygon's vertex when clicking on
a vertex of a polygon? I want to use this to delete verteces by
clicking on them.

There is currently no event fired when a user clicks on a control point on an editable polygon.
 

2. What event is fired if a polygon has been changed, ie by dragging a
vertex?


You attach a set_at listener on the polygon's path, e.g.:

google.maps.event.addListener(polygon.getPath(), 'set_at', function(index) {
  // add code here.
});

Enoch

Adrien

unread,
Nov 21, 2011, 4:42:38 AM11/21/11
to Google Maps JavaScript API v3
Thank you very much Enoch, you cleared a lot of doubts

Is there any way you can think of which would allow me to delete
vertexes by clicking on them? Or is Google working at exposing the
vertex-clicking event?


Adrien
On Nov 20, 4:01 pm, "Enoch Lau (Google Employee)"


<enoch...@google.com> wrote:
> > 1. How to capture the index id of a polygon's vertex when clicking on
> > a vertex of a polygon? I want to use this to delete verteces by
> > clicking on them.
>
> There is currently no event fired when a user clicks on a control point on
> an editable polygon.
>
> > 2. What event is fired if a polygon has been changed, ie by dragging a
> > vertex?
>

>  http://code.google.com/apis/maps/documentation/javascript/overlays.ht...

Enoch Lau (Google Employee)

unread,
Nov 21, 2011, 5:41:37 AM11/21/11
to google-map...@googlegroups.com
If an event for clicking control points would be useful to you, or more generally a built-in solution for deleting vertices, feel free to add these as feature requests on the issue tracker.

Because you can't get click events on control points, one way to implement delete would be to have users drag the vertex they want to delete on top of an adjacent vertex. In your 'set_at' listener, you can look at whether the dragged vertex is now sufficiently close (you can define the threshold) to one of its neighbouring vertices; if so, delete it.

Enoch

Marcelo

unread,
Nov 21, 2011, 9:21:05 AM11/21/11
to Google Maps JavaScript API v3
On Nov 21, 2:42 am, Adrien <ahing...@gmail.com> wrote:
> Is there any way you can think of which would allow me to delete
> vertexes by clicking on them?

Right click anywhere to delete the closest node:
http://maps.forum.nu/v3/gm_editable_polygon.html

You can add a threshold if you like, so that the node only gets
deleted if the click takes place at a distance below that threshold.

--
Marcelo - http://maps.forum.nu
--

Adrien

unread,
Nov 21, 2011, 1:32:50 PM11/21/11
to Google Maps JavaScript API v3
@Enoch, thanks for the feedback, I have already added the request. I
however believe your workaround for deleting verteces not user
friendly enough.

@Marcelo, very interesting, but this will not work with v3 of the
GMaps, or am I mistaken?


Adrien


On Nov 21, 3:21 pm, Marcelo <marcelo...@hotmail.com> wrote:
> On Nov 21, 2:42 am, Adrien <ahing...@gmail.com> wrote:
>
> > Is there any way you can think of which would allow me to delete
> > vertexes by clicking on them?
>
> Right click anywhere to delete the closest node:http://maps.forum.nu/v3/gm_editable_polygon.html
>
> You can add a threshold if you like, so that the node only gets
> deleted if the click takes place at a distance below that threshold.
>
> --

> Marcelo -http://maps.forum.nu

Marcelo

unread,
Nov 21, 2011, 1:41:00 PM11/21/11
to Google Maps JavaScript API v3
On Nov 21, 11:32 am, Adrien <ahing...@gmail.com> wrote:
>
> @Marcelo, very interesting, but this will not work with v3 of the
> GMaps, or am I mistaken?
>

Huh? What version did I use in the demo?

--
Marcelo - http://maps.forum.nu
--

Adrien

unread,
Nov 21, 2011, 2:30:35 PM11/21/11
to Google Maps JavaScript API v3
@Marcelo, sorry I'm an idiot. I saw the G prefix and thought it was
v2. This looks very promising.
I tested it and it works great. I have one question though: why does
rightclicking on the vertex actually fire the click, while changing
the venet from "rightclick" to "click" will not fire the event on the
vertex?

As far as I can see, if I can change this to a left click I have my
solution.


Adrien

On Nov 21, 7:41 pm, Marcelo <marcelo...@hotmail.com> wrote:
> On Nov 21, 11:32 am, Adrien <ahing...@gmail.com> wrote:
>
>
>
> > @Marcelo, very interesting, but this will not work with v3 of the
> > GMaps, or am I mistaken?
>
> Huh? What version did I use in the demo?
>
> --

> Marcelo -http://maps.forum.nu

Marcelo

unread,
Nov 21, 2011, 2:56:00 PM11/21/11
to Google Maps JavaScript API v3
On Nov 21, 12:30 pm, Adrien <ahing...@gmail.com> wrote:
> I tested it and it works great. I have one question though: why does
> rightclicking on the vertex actually fire the click, while changing
> the venet from "rightclick" to "click" will not fire the event on the
> vertex?

Because the polygon has clickable: false.
If you set clickable: true then the polygon will absorb the click and
it will not go through to the map.
Note that the rightclick event is attached to the map, and not to the
polygon.

--
Marcelo - http://maps.forum.nu
--

>

Adrien

unread,
Nov 21, 2011, 5:01:11 PM11/21/11
to Google Maps JavaScript API v3
Thanks Marcelo.
Is there any way that a left-click on a vertex will actually delete
it?


Adrien

On Nov 21, 8:56 pm, Marcelo <marcelo...@hotmail.com> wrote:
> On Nov 21, 12:30 pm, Adrien <ahing...@gmail.com> wrote:
>
> > I tested it and it works great. I have one question though: why does
> > rightclicking on the vertex actually fire the click, while changing
> > the venet from "rightclick" to "click" will not fire the event on the
> > vertex?
>
> Because the polygon has clickable: false.
> If you set clickable: true then the polygon will absorb the click and
> it will not go through to the map.
> Note that the rightclick event is attached to the map, and not to the
> polygon.
>
> --

> Marcelo -http://maps.forum.nu

Marcelo

unread,
Nov 21, 2011, 5:27:26 PM11/21/11
to Google Maps JavaScript API v3
On Nov 21, 3:01 pm, Adrien <ahing...@gmail.com> wrote:
> Is there any way that a left-click on a vertex will actually delete
> it?

As Enoch already told you "There is currently no event fired when a
user clicks on a control point on an editable polygon.", so no, there
is no way to do it with the events that are currently exposed.

But everything is possible! You can climb the DOM tree, find the DIVs
that represent the nodes and attach event handlers to them, but the
code will likely break when Google release future versions of the API,
as there is no guarantee that the DOM tree structure will remain the
same.

--
Marcelo - http://maps.forum.nu

Enoch Lau (Google Employee)

unread,
Nov 21, 2011, 5:48:46 PM11/21/11
to google-map...@googlegroups.com

But everything is possible! You can climb the DOM tree, find the DIVs
that represent the nodes and attach event handlers to them, but the
code will likely break when Google release future versions of the API,
as there is no guarantee that the DOM tree structure will remain the
same.

Yes, it's not a good idea to do this.

Enoch

Adrien

unread,
Nov 21, 2011, 6:30:05 PM11/21/11
to Google Maps JavaScript API v3
Ok, clear. Thank you very much for your support. It was really great!

On Nov 21, 11:48 pm, "Enoch Lau (Google Employee)"

Reply all
Reply to author
Forward
0 new messages