Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

trigger google maps marker click

I have a google map set up to find the user's current location, and center the map at that closest marker to their location. The markers, when clicked, open up an infobox (note this is a little different than an infoWindow - http://google-maps-utility-library-v3.googlecode.com/svn/trunk/infobox/docs/examples.html). What I want to do, however, is automatically open up the nearest marker without the user actually clicking. Here's the code to trigger a marker opening:

//loop through all locations to add a marker: addMarker = function(loc) {     var markerLoc = new google.maps.LatLng(loc.Lat, loc.Long);      var marker = new google.maps.Marker({         map: map,         position: markerLoc     });      google.maps.event.addListener(marker, "mousedown", function() {          var infoOpts = {             content: loc.markerText,             boxStyle: { background: "none transparent", width: "180px"},             pixelOffset: new google.maps.Size(-90, 0),             closeBoxMargin: "5px"         };          var ib = new InfoBox(infoOpts);         ib.open(map, marker);     });      markers.push(marker); }; 

So somehow I have to trigger the mouseDown function of the appropriate marker, but it has to be done in a function outside of this one. I will have a reference to the appropriate marker in the array (markers[closestmarker]).

like image 748
mheavers Avatar asked Jul 22 '11 18:07

mheavers


People also ask

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.

Can I put a marker on Google Maps?

You can now pinpoint locations manually by clicking the marker icon and placing it directly onto the map, or search for locations using the search box at the top of the screen. If you're adding locations manually, you can name the location and save to add it to the map.


2 Answers

i see that this question has been sitting for quite awhile, but, just in case, this answer may be helpful: trigger google maps marker click

The code would look like this:

var marker = new google.maps.Marker({}); new google.maps.event.trigger( marker, 'click' ); 

Good luck!

like image 93
kevinstueber Avatar answered Sep 29 '22 01:09

kevinstueber


I found I needed to attach a click event to the marker like so

var marker = new google.maps.Marker({}); marker.addListener('click', function() {     infowindow.open(map, marker); }); new google.maps.event.trigger( marker, 'click' ); 
like image 24
A Averill Avatar answered Sep 29 '22 01:09

A Averill