Clicking polygons trigger map click event

2,843 views
Skip to first unread message

kilkul

unread,
Jun 17, 2010, 4:39:37 AM6/17/10
to Google Maps JavaScript API v3
I've noticed that polygons click events trigger the map click event,
also. As there doesn't seem to be any way of determining, on map's
click event, if the target was (or not) an overlay... how can I fix
this?

How can I trigger a polygon's click event, WITHOUT triggering the
map's click event, too (which has its own functionality, in my case)?

ClubPit

unread,
Jun 17, 2010, 5:22:36 AM6/17/10
to Google Maps JavaScript API v3
It's not the cleanest solution, but if you checked if the LatLng of
the click event was
within the boundaries of the polygon it would have the same effect. If
it's within the boundary,
it's a polygon event, if it's not within the boundary it's a map
event. Ugly, but it would work.

KW

kilkul

unread,
Jun 17, 2010, 6:03:58 AM6/17/10
to Google Maps JavaScript API v3
Yeah, thanks, I'll try that. Indeed, should do the trick. But... isn't
there any "native" way of doing this? 'Cause if not, this is a real
bug.

kilkul

unread,
Jun 17, 2010, 6:51:17 AM6/17/10
to Google Maps JavaScript API v3
To whom it may concern: since there is no native Polygon.getBounds()
method, you may use

google.maps.Polygon.prototype.getBounds = function(){
var bounds = new google.maps.LatLngBounds;
this.getPath().forEach(function(latLng){
bounds.extend(latLng);
});

return bounds;
};

to support it (more or less) as the API V2 did...

Rossko

unread,
Jun 17, 2010, 7:35:36 AM6/17/10
to Google Maps JavaScript API v3
>     google.maps.Polygon.prototype.getBounds = function(){
>         var bounds = new google.maps.LatLngBounds;
>         this.getPath().forEach(function(latLng){
>                 bounds.extend(latLng);
>             });
>
>         return bounds;
>     };

Bounds is not the same as a polygon. Using that function, it is
possible to click close to, but not inside, a polygon ... and yet be
inside the bounds.

Jason Sanford

unread,
Jun 17, 2010, 8:30:45 AM6/17/10
to google-map...@googlegroups.com
I solved this problem by temporarily removing the map's 'click' event on polygon 'mousedown' and then re-creating it on polygon 'mouseup'.

--
You received this message because you are subscribed to the Google Groups "Google Maps JavaScript API v3" group.
To post to this group, send email to google-map...@googlegroups.com.
To unsubscribe from this group, send email to google-maps-js-a...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-maps-js-api-v3?hl=en.


pankaj kumar jha

unread,
Jun 17, 2010, 8:17:10 AM6/17/10
to google-map...@googlegroups.com
i want to show my own logo( an image file .jpg) in place of markers.
how i can do so?
 
give some hint or any link.

--
You received this message because you are subscribed to the Google Groups "Google Maps JavaScript API v3" group.
To post to this group, send email to google-map...@googlegroups.com.
To unsubscribe from this group, send email to google-maps-js-a...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-maps-js-api-v3?hl=en.




--
pankaj kumar jha
9591917652

Nathan Raley

unread,
Jun 17, 2010, 9:05:46 AM6/17/10
to google-map...@googlegroups.com
You would use the marker.setIcon() property and pass it the link to the image you want.  The basic google documenation walks you through setting one up in a couple of different methods.  You have the setIcon method or you can create your own markerimage.  Both of which are documented in the regular google reference.

pi5701

unread,
Jun 17, 2010, 11:19:40 AM6/17/10
to Google Maps JavaScript API v3
On 17 Jun., 14:30, Jason Sanford <jasonsanf...@gmail.com> wrote:
I solved this problem by temporarily removing the map's 'click' event
on
polygon 'mousedown' and then re-creating it on polygon 'mouseup'.
A little documentation can be found here:http://geojason.info/2010/04/
map-and-polygon-click-events-google-maps...


nice solution.
i have the same problem with kml layers.
unfortunately there is no mouseup/ down event available for kml
layers.

made a feature request here:
http://code.google.com/p/gmaps-api-issues/issues/detail?id=2499&q=apitype%3AJavascript3&colspec=ID%20Type%20Status%20Introduced%20Fixed%20Summary%20Internal%20Stars&start=100

William

unread,
Jun 18, 2010, 3:16:41 AM6/18/10
to Google Maps JavaScript API v3
On Jun 17, 6:39 pm, kilkul <kil...@gmail.com> wrote:
> I've noticed that polygons click events trigger the map click event,
> also. As there doesn't seem to be any way of determining, on map's
> click event, if the target was (or not) an overlay... how can I fix
> this?
>

an indirect method is to check the cursor style: if it's a pointer
then it's an overlay. However this method won't work on phones
because they don't have cursors.

I tried it with a mousedown listener on the map_canvas to record the
cursor style. Then it's used in the map click listener to check if
the click was on an overlay.

see the following examples:

Polygon
http://www.william-map.com/20100618/1/polygon.htm

KML
http://www.william-map.com/20100618/1/kml.htm

pi5701

unread,
Jun 18, 2010, 9:49:09 AM6/18/10
to Google Maps JavaScript API v3
thx a lot William!

kilkul

unread,
Jun 23, 2010, 3:04:12 PM6/23/10
to Google Maps JavaScript API v3
Indeed, but my getBounds method was somewhat "outside" of this topic -
just a good method for getting a polygon's bounds.

And, since my polygons are pretty regular (close to a circle), their
bounds have are enough precision for my needs. Still, the solutions
below seem nice (and even more precise), too.


On Jun 17, 12:35 pm, Rossko <ros...@culzean.clara.co.uk> wrote:
> >     google.maps.Polygon.prototype.getBounds = function(){
> >         var bounds = new google.maps.LatLngBounds;
> >         this.getPath().forEach(function(latLng){
> >                 bounds.extend(latLng);
> >             });
>
> >         return bounds;
> >     };
>
> Bounds is not the same as a polygon.  Using that function, it is
> possible toclickclose to, but not inside, a polygon ... and yet be
> inside the bounds.
Reply all
Reply to author
Forward
0 new messages