Maps API for GWT: How to get the clicked polyline vertex?

279 views
Skip to first unread message

Brian

unread,
Oct 12, 2008, 11:15:01 AM10/12/08
to Google Web Toolkit
When editing a polyline, how do you get the clicked vertex?

I've got a polyline that's editable. When clicking a vertex, I want
to delete that vertex. My polyline is set up as:

map.addOverlay(poly);
poly.setEditingEnabled(true);
poly.setDrawingEnabled();

Adding a PolyLineClickHandler will tell me which polyline was clicked,
but how do you get the vertex from that? After I get the vertex, I
want to get the vertex's index, so I can then call:
poly.deleteVertex(index)

The javascript Google maps sample here:
http://gmaps-samples.googlecode.com/svn/trunk/poly/mymapstoolbar.html

does it like so:

GEvent.addListener(poly, "click", function(latlng, index) {
if (typeof index == "number") { // this is what I'm looking for in
the Maps API for GWT
poly.deleteVertex(index);
} else {
// Other code
}

That 'deleteVertex when vertex clicked' is what I'm trying to port to
my gwt app, basically.

Thanks.

Eric Ayers

unread,
Oct 13, 2008, 2:30:22 PM10/13/08
to Google-We...@googlegroups.com
Hi Brian,

Thanks for bringing this up. I checked at:

http://code.google.com/apis/maps/documentation/reference.html#GPolygon

and I don't see the 'index' parameter documented for the polyline
'click' event. At present, I'm not adding bindings for any
undocumented features (there are many of them!) I noticed that on FF3
on Linux, the polyline example in the site you sent me fails
miserably... maybe that's why it isn't documented yet?

Anyway, if you'd like to see this feature published, you might want to
lobby for it on the gwt-maps mailing list. Please read their
guidelines before posting.

Regards,
-Eric.

--
Eric Z. Ayers - GWT Team - Atlanta, GA USA
http://code.google.com/webtoolkit/

Eric Ayers

unread,
Oct 13, 2008, 2:31:24 PM10/13/08
to Google-We...@googlegroups.com
Um, sorry about that - there is no gwt-maps list, I mean the Google
Maps API group

http://groups.google.com/group/Google-Maps-API

-Eric.

Brian

unread,
Oct 13, 2008, 3:19:53 PM10/13/08
to Google Web Toolkit
Hi Eric,

Thanks for the reply. Yeah I couldn't find where the 'index"
parameter was documented in the Javascript API, so I wasn't sure if
the docs were stale, or that sample was experimental. Now I know..

For what it's worth, I was able to do what I needed to by looping
through all the verts. In case anyone else is looking to do this,
here's the code:

This is the 'onClick' handler for a polyline. The polyline is 'poly'

int vertCount = poly.getVertexCount();
if (vertCount ==2 ) {
// maps rejects vertex deletion when you'd end up with a point
// hack to fix:
poly.deleteVertex(1);
poly.deleteVertex(0);
poly.setEditingEnabled(false);
map.removeOverlay(poly);
return;
}

LatLng clickedLatLng = event.getLatLng();
for (int i = 0; i < vertCount; i++) {
if ( clickedLatLng.isEquals((poly.getVertex(i))) ){
poly.deleteVertex(i);
break;
}
}

-- I initally thought that the latlng compare would fail, as the level
of precision is so high, it seemed nearly impossible for anyone to
click the exact same place twice. Turns out though that when a vertex
is clicked, even if it's somewhere within the vertex 'box' it always
returns the latlng of the original vert clicked -- sweet.

P.S. While I'm thinking about maps... the sample code on the maps for
gwt website showing how to bring in the js script in the index.html
using a dummy key for localhost didn't work -- I had to generate a key
from the maps api site for "localhost:8888" in order to run in hosted
mode. Otherwise, it just popped a message box saying the key was
invalid.


On Oct 13, 2:31 pm, "Eric Ayers" <zun...@google.com> wrote:
> Um, sorry about that - there is no gwt-maps list, I mean the Google
> Maps API group
>
>  http://groups.google.com/group/Google-Maps-API
>
> -Eric.
>
>
>
> On Mon, Oct 13, 2008 at 2:30 PM, Eric Ayers <zun...@google.com> wrote:
> > Hi Brian,
>
> > Thanks for bringing this up.  I checked at:
>
> >  http://code.google.com/apis/maps/documentation/reference.html#GPolygon
>
> > and I don't see the 'index' parameter documented for the polyline
> > 'click' event.    At present, I'm not adding bindings for any
> > undocumented features (there are many of them!)  I noticed that on FF3
> > on Linux, the polyline example in the site you sent me fails
> > miserably... maybe that's why it isn't documented yet?
>
> > Anyway, if you'd like to see this feature published, you might want to
> > lobby for it on the gwt-maps mailing list.  Please read their
> > guidelines before posting.
>
> > Regards,
> > -Eric.
>

Eric Ayers

unread,
Oct 13, 2008, 3:23:07 PM10/13/08
to Google-We...@googlegroups.com

I asked the Maps team about that once. Apparently there are some
situations where you need a real key and what you did is the
recommended solution for it.

Reply all
Reply to author
Forward
0 new messages