Wednesday, June 5, 2024
 Popular · Latest · Hot · Upcoming
105
rated 0 times [  109] [ 4]  / answers: 1 / hits: 69575  / 13 Years ago, fri, july 22, 2011, 12:00:00

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]).


More From » google-maps

 Answers
31

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' );

[#91051] Thursday, July 21, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jack

Total Points: 557
Total Questions: 96
Total Answers: 80

Location: Saint Helena
Member since Mon, Jan 16, 2023
1 Year ago
jack questions
Sun, Jan 31, 21, 00:00, 3 Years ago
Thu, Dec 10, 20, 00:00, 4 Years ago
Sat, Aug 1, 20, 00:00, 4 Years ago
Fri, Apr 17, 20, 00:00, 4 Years ago
Wed, Aug 14, 19, 00:00, 5 Years ago
;