Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I detect a click event on a google maps PolyLine overlay?

It seems to me that I should be able to do the following to detect a click event on a line on a google map:

var line = new GPolyline( ... );
map.addOverlay(line);
GEvent.addListener(line, "click", function(latlng){ alert("clicked"); });

The api reference says this is available in version 2.88, which was released in 2007(!?), so I'm assuming that's what I'm using, but I don't know how to confirm that.

I also tried setting the {clickable:true} option explicitly (it's supposed to be the default.) I've tested in FireFox 3 and Opera 9.6 so doubt it's browser specific. I'm also using jQuery on the page.

I have plenty of code detecting clicks on markers that works fine, clicking on lines would be really nice, can anyone enlighten me?

like image 258
Tom Avatar asked Apr 06 '09 10:04

Tom


People also ask

How do you call the marker click function from outside the map?

addListener(marker, 'click', (function(marker, i) { return function() { infowindow. setContent(locations[i][0]); infowindow. open(map, marker); } })(marker, i)); iconCounter++; } function AutoCenter() { // Create a new viewpoint bound var bounds = new google.

How do I add an event to Google Maps?

To add an event, head to Google Maps on Android, tap on Contribute >Events > Add a public event. You can add an event name, tag the location, and add the time and date of the event as well. There's an option to add an image, write more event details, and add description as well.

Which event is triggered when the map is continuously moved and tracked?

Then you will have a dragend event available.


2 Answers

Update: in version 3 of the API, you want to use google.maps.event.addListener(object, event, function);

e.g.

google.maps.event.addListener(polyline, 'click', function() {
    alert('you clicked polyline');
    });

For more details, see the events api

like image 111
Benubird Avatar answered Oct 09 '22 17:10

Benubird


I just did a quick test and the following code worked on my test page:

var polyline = new GPolyline([
  new GLatLng(37.4419, -122.1419),
  new GLatLng(37.4519, -122.1519)
], "#ff0000", 10);
map.addOverlay(polyline);

GEvent.addListener(polyline, 'click', function() {
    alert('you clicked polyline');
});

The way to tell what version of google maps you have is to look at the v= parameter of the google maps src url you have

http://maps.google.com/maps?file=api&v=2&key=MY_API_KEY

In this case I have "v=2", that means I am using the latest stable 2 version, which supports clickable polylines (as of today 2.101 is the most recent release). "v=2.x" means you are using an edge release. And any "v=2.5" where the anything after the period (.) is a number refers to a specific release

like image 23
howardr Avatar answered Oct 09 '22 16:10

howardr