8

I am working with Google Maps API V3. The googlemaps displays markers for some places/location by default, I need to capture the click event when one of them is clicked.. enter image description here

I have tried to use the click event of the map but it not works because the user is clicking the marker not the map, code is given:

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

Can someone give me an idea how to do it?

EDIT: I can find no suitable Event in the available Events, which can help me!

Please note that I am not talking about the custom Markers (Created by User), these are default markers, googlemaps displays them by default.

Yaqub Ahmad
  • 27,569
  • 23
  • 102
  • 149
  • 1
    There is no way in the Google Maps API v3 at present to access the click events on those markers. The only option is to hide them using styled maps. http://stackoverflow.com/questions/8902829/disable-click-behavior-of-poi-markers-in-google-maps-v3-6 – geocodezip May 07 '13 at 12:08
  • Yaqub, the answer [here](http://stackoverflow.com/questions/15325283/is-there-a-way-to-detect-canvas-lines-with-jquery) a long way from what you want but possibly provides a way ahead. – Beetroot-Beetroot Oct 09 '13 at 10:02
  • [this answer](http://stackoverflow.com/a/24234818/1779797) presents a workaround – Tesseract May 02 '15 at 15:08

4 Answers4

1

Here's a clean solution straight from Google: https://developers.google.com/maps/documentation/javascript/examples/event-poi

You can easily customize the default infowindow for POI's. Just ignore the route-making feature of the tutorial.

Kalnode
  • 9,386
  • 3
  • 34
  • 62
0

I have never tried it but could you retrieve the list of places that Google has displayed using the Google Places API and then establish your own pins and click events for them?

MymsMan
  • 191
  • 8
  • I am exactly doing the same and in that scenario there is no issue, I can play with them. The markers I am talking about exists on map without any search; you don't need to search them, they exists by default. – Yaqub Ahmad May 07 '13 at 16:05
0

It's a bit hacky, and doesn't work on IE < 9, but you can listen on dom event, to detect the creation of the window, using

Mutation Observer

Here is a plunkr to demonstrate : http://plnkr.co/edit/pGep9OZFligLhRtHlhgk You can check in the console, an event is fired (actually twice) when you click on a POI.

vianney
  • 380
  • 1
  • 4
  • 12
-2

Note that the OP is asking about doing this using the GMAP3 library, which is a jQuery plugin library that adds an API layer on top of the Google Maps layer. it is absolutely correct to say that the Google API is to use google.maps.addListener, but I think that the OP wants something that's closer to the GMAP3 example using addMarkers, but listening to the click event. With that in mind, I modified the example from GMAP3 and changed the mouseenter event to the click event and got rid of the mouseleave event.

Refer

Community
  • 1
  • 1
Shail
  • 1,565
  • 11
  • 24
  • I am not talking about Custom Markers (Created by the user), I am talking about default markers, google display them for certain places/locations. – Yaqub Ahmad May 07 '13 at 11:41