Skip to content
This repository has been archived by the owner on Nov 9, 2023. It is now read-only.

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

Closed
neotamizhan opened this issue Jun 10, 2012 · 14 comments
Closed

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

neotamizhan opened this issue Jun 10, 2012 · 14 comments

Comments

@neotamizhan
Copy link

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
Copy link
Owner

hpneo commented Jun 10, 2012

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

@neotamizhan
Copy link
Author

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
Copy link
Owner

hpneo commented Jun 10, 2012

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

@neotamizhan
Copy link
Author

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
Copy link

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
Copy link

How to click map location instead of marker

@kamleshwebtech
Copy link

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
Copy link
Owner

hpneo commented Sep 26, 2020

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
Copy link

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

@kamleshwebtech
Copy link

@kavishatalsania
Copy link

kavishatalsania commented Jan 6, 2022

@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
Copy link
Owner

hpneo commented Jan 7, 2022

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

@kavishatalsania
Copy link

@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
Copy link
Owner

hpneo commented Jan 10, 2022

@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.

Repository owner locked as resolved and limited conversation to collaborators Jan 10, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants