Triggering a click on a kml polygon

2,047 views
Skip to first unread message

fastapps

unread,
Aug 2, 2010, 9:52:23 AM8/2/10
to Google Maps JavaScript API v3
I would like to display the infoWindow for a kml polygon automatically
on loading the layer. To do this I am trying to trigger a click on
the polygon. In order to specify the location I pass the LatLng to
the trigger.

map = new google.maps.Map(document.getElementById("map_canvas"),
myOptions);
ctaLayer = new google.maps.KmlLayer('http://www.xxxx.xx.uk/xml/
newbinsgr.kml');

google.maps.event.addListener(ctaLayer,'click',function(kmlEvent)
{
});

var aLatLng = new google.maps.LatLng(51.5, 0.15);
google.maps.event.addListener(map,'click',aLatLng)

There are 2 problems 1) the infowindow does not dsplay 2) the only
data that gets passed to the listener is that which is passed to the
trigger call ie I only get what I already know !

Is this correct ?

(If I click on the polygon with the mouse I get the infowindow
displayed and the listener receives the data for the polygon.)

Thanks

Rossko

unread,
Aug 2, 2010, 6:21:41 PM8/2/10
to Google Maps JavaScript API v3
> I would like to display the infoWindow for a kml polygon automatically
> on loading the layer.  To do this I am trying to trigger a click on
> the polygon.  In order to specify the location I pass the LatLng to
> the trigger.

I don't see anywhere in your code snippet that you trigger any event.

Lenius

unread,
Aug 3, 2010, 3:01:49 AM8/3/10
to Google Maps JavaScript API v3
Hi,

I think you can use this example :

map = new
google.maps.Map(document.getElementById("map_canvas"),myOptions);

ctaLayer = new google.maps.KmlLayer(''http://www.xxxx.xx.uk/xml/
newbinsgr.kml',{suppressInfoWindows:true,preserveViewport:true});
google.maps.event.addListener(ctaLayer, 'click', function(obj) {
var marker = new google.maps.Marker({position: obj.position});
infowindow.setContent(obj.position);
infowindow.setPosition(obj.position);
infowindow.open(map);
});

ctaLayer.setMap(map);

/carsten

fastapps

unread,
Aug 3, 2010, 3:23:29 AM8/3/10
to Google Maps JavaScript API v3
Sorry, not sure how that got left off. Here it is

var aLatLng = new google.maps.LatLng(51.5, 0.15);
var kmlFD = {
name: 'test name'
};
var kmlME = {
featureData: kmlFD,
latLNG: aLatLng
};
google.maps.event.trigger(ctaLayer ,'click',kmlME,aLatLng);

I have tried many combinations of parameters (more and less) but
basically, whatever I see in the listener is what I pass in the
trigger and that seems to defeat the purpose of the trigger. All I
want to do is simulate a mouse click at a specified point on the map
(kml layer) and let that initiate the display of an infowindow.

fastapps

unread,
Aug 3, 2010, 4:01:42 AM8/3/10
to Google Maps JavaScript API v3
Hi there,
In your example, how does obj get populated with the placemark data
from the kml file ?

If I initiate a trigger,
google.maps.event.trigger(ctaLayer ,'click');
then obj is undefined (as there is no parameter after the 'click'
parameter.
If I add a 3rd parameter
google.maps.event.trigger(ctaLayer ,'click',null);
then obj is null ie the listener only receives what is sent by the
trigger.

This does not appear to be the correct behaviour.

Thanks

Rossko

unread,
Aug 3, 2010, 5:38:25 AM8/3/10
to Google Maps JavaScript API v3
> If I add a 3rd parameter
>     google.maps.event.trigger(ctaLayer ,'click',null);
> then obj is null   ie the listener only receives what is sent by the
> trigger.
>
> This does not appear to be the correct behaviour.

I'm not sure why you think that is incorrect. What would you expect
it to send, instead? How would the trigger guess that was what you
had in mind, rather than the day of the week or the word "boo"?
http://code.google.com/apis/maps/documentation/javascript/reference.html#event
google.maps.event.trigger( AnyObjectYouLike,
'AnyEventNameYouLike',
AnyArgumentYouLike) ;

I do think you are on the wrong track here altogether; the polygon
object within the KmlLayer is not accessible individually, so I do not
think you can successfully simulate a click on it.

fastapps

unread,
Aug 3, 2010, 6:01:59 AM8/3/10
to Google Maps JavaScript API v3
I guess in my naive logic, if I use the mouse to click on the kml
layer (at the specific location I require) it knows which polygon is
underneath the click, and pops up the infowindow containing the name
and description from that polygon's kml entry. Therefore, if I merely
trigger a click at a specific point (rather than with a mouse) I would
expect it to respond in a similar manner.

If all it does is display the info that you pass to it and as you
point out, kml polygons are not individually referable so that info
about the polygon is not known, then being able to trigger a click on
a kml layer seems to have no useable function.

But as you say, I may be on completely the wrong track !

Thanks

On Aug 3, 10:38 am, Rossko <ros...@culzean.clara.co.uk> wrote:
> > If I add a 3rd parameter
> >     google.maps.event.trigger(ctaLayer ,'click',null);
> > then obj is null   ie the listener only receives what is sent by the
> > trigger.
>
> > This does not appear to be the correct behaviour.
>
> I'm not sure why you think that is incorrect.   What would you expect
> it to send, instead?  How would the trigger guess that was what you
> had in mind, rather than the day of the week or the word "boo"?
>  http://code.google.com/apis/maps/documentation/javascript/reference.h...

Rossko

unread,
Aug 3, 2010, 7:13:45 AM8/3/10
to Google Maps JavaScript API v3
> I guess in my naive logic, if I use the mouse to click on the kml
> layer (at the specific location I require) it knows which polygon is
> underneath the click

Yup, you are correct there, the API can work out which poly from the
mouse position. The 'missing link' is that you (your own code) have
no means to find out which poly is at some arbritary position, the
KmlLayer mechanism doesn't share that info.

I don't know if simulating a mouse click to a KmlLayer object (rather
than a specific poly) will give the same effect as clicking on the
screen, I suspect not but maybe someone who understands the layer
mechanism better will comment. To successfully simulate such a click
you'd have to make your event trigger supply the same sort of
parameters as a real click - x/y pixels perhaps?

> being able to trigger a click on
> a kml layer seems to have no useable function.

Entirely possible ; the trigger method allows you to trigger arbritary
events on any object you like. Only some objects listen for
events,and then only for specific events.
var x = 2;
google.maps.event.trigger( x, 'explosion', parameter);
is perfectly valid, but nothing happens, numbers don't respond to
events unless you have set up a listener.

Still waiting for your demo of simulated click on a KmlLayer to see
what actually happens...

fastapps

unread,
Aug 3, 2010, 8:23:43 AM8/3/10
to Google Maps JavaScript API v3
There is a demo at http://www.bexley.gov.uk/xml/testlink6.htm

There is a 10 second wait between the map displaying and the trigger
test. The click is on the bottom-left polygon.

Rossko

unread,
Aug 3, 2010, 8:53:39 AM8/3/10
to Google Maps JavaScript API v3
But you've put your own click listener on the KmlLayer - which does
get triggered and produces the alert box.
I thought you were trying to trigger the 'native' click handling of
the KmlLayer in the hope that it would in turn trigger a poly click?

fastapps

unread,
Aug 3, 2010, 9:24:07 AM8/3/10
to Google Maps JavaScript API v3
Yes, but just to monitor what was happening. I have removed the
listener in testlink7.htm. Same (non) effect.
Reply all
Reply to author
Forward
0 new messages