hpneo / gmaps

the easiest way to use Google Maps
https://hpneo.dev/gmaps/
7.03k stars 1.28k forks source link

How to call the marker.click function from outside the map? #40

Closed neotamizhan closed 12 years ago

neotamizhan commented 12 years ago

Hi,

This is not an issue, just a query.

I am building a map with several markers (and infoWindows). Now when a marker is clicked, the corresponding infoWindow is displayed (with proper screen positioning). I want to do the same when a place is selected from a drop down list. How can I hook into the google.maps.Marker's click event?

I can call, "map.hideInfoWindows()" followed by "marker[i].infoWindow.open". This does show the info window. But the displayed widow is not centered. And also it does not have any of the events associated with it. I cannot close this window using the "close" button.

Could you please help me with this?

Siddharth

hpneo commented 12 years ago

Hi, you can use marker.click(marker) to trigger the marker's click event.

neotamizhan commented 12 years ago

Thanks for the quick response.

I tried using the click event. I get the following error: TypeError: Object #<W> has no method 'click'.

I guess the way i extract the markers is wrong. Right now I do it like this, var myMarker = map.markers[9] and then I call, myMarker.click(myMarker)

Is there a correct way of extracting the marker object?

Thanks again for the quick reply.

hpneo commented 12 years ago

That's the right way, it shouldn't throw any error. I'm gonna check this bug.

neotamizhan commented 12 years ago

Hi,

The issue was solved. Calling the marker's click event using the google.maps.event.trigger method solved the issue.

 var ourMarker = getMarker(title)
 google.maps.event.trigger(ourMarker, 'click');
codingkart commented 10 years ago

function addMarker(lat, lng, info ,color) { var pt = new google.maps.LatLng(lat, lng); //bounds.extend(pt); if(color) { var cl="F1C40F"; } else { var cl="555555";
} var marker = new google.maps.Marker({ position: pt, icon: 'https://chart.googleapis.com/chart?chst=d_map_pin_letter&chld='+ (info) +'|' + cl +'|ffffff', map: map });

}

hello

my issue is that , i would like to change the marker color on mouse over on listing , so how can i bind the our listing to marker ?

zia-qamar commented 6 years ago

How to click map location instead of marker

kamleshwebtech commented 4 years ago

Hi,

The issue was solved. Calling the marker's click event using the google.maps.event.trigger method solved the issue.

 var ourMarker = getMarker(title)
 google.maps.event.trigger(ourMarker, 'click');

This does not work in clustered markers (one of the group markers) situation. Please share suggestion if you have. Thanks.

hpneo commented 4 years ago

Hi @kamleshwebtech you'll probably need to attach your click events into the marker cluster, since the markers aren't in the map when they grouped.

Check this example: https://googlemaps.github.io/v3-utility-library/packages/markerclustererplus/examples/events_example.html

kavishatalsania commented 2 years ago

Hi @kamleshwebtech and @hpneo , I am still not able to trigger click event on clustered markers. Can you share example code if possible?

kamleshwebtech commented 2 years ago

@kavishatalsania Please check this demo https://www.mrhandyman.com/local-handyman-service

kavishatalsania commented 2 years ago

@kamleshwebtech Thank you sharing this link. I want to open a info window of a marker which is inside map cluster. For other marker google.maps.event.trigger(ourMarker, 'click'); is working, but if marker is inside cluster then it is not working. Can you share the code to make it work?

hpneo commented 2 years ago

@kavishatalsania do you have a working example where I can debug?

kavishatalsania commented 2 years ago

@hpneo This is the working example: https://codesandbox.io/s/new-bird-8fp0q?file=/src/index.ts

On line 58, I triggered event for marker[0] which is not inside any marker cluster. That is why it it working as expected.

Please change line number 58 to google.maps.event.trigger(markers[1], "click"); . That will not work because it is inside marker cluster.

hpneo commented 2 years ago

@kavishatalsania I'm sorry but that example doesn't event use GMaps. If you are using GMaps with MarkerClusterer I recommend you to create a new issue.