I'm working on a problem involving Google Maps API and infowindows. When I click on a marker, an infowindow pops up, and I am trying to make it so that when that happens, the lat/lng for that marker is displayed in the infowindow. The lat/lng in this case being stored in the stops array:
<script type="text/javascript"
src="http://maps.googleapis.com/maps/api/js?sensor=false">
</script>
<script type="text/javascript">
var map;
var marker;
var infowindow; //Global infowindow created
function initialize() {
var latlng = new google.maps.LatLng(37.784, -122.408);
var stops = new Array();
stops = [
new google.maps.LatLng(37.7655, -122.4525899),
new google.maps.LatLng(37.7649999, -122.45656),
new google.maps.LatLng(37.7643, -122.4608199),
];
var myOptions = {
zoom: 10,
mapTypeId: google.maps.MapTypeId.ROADMAP,
center: new google.maps.LatLng(37.7655, -122.4525899)
};
map = new google.maps.Map(document.getElementById("content"),
myOptions);
infowindow = new google.maps.InfoWindow({ //infowindow options set
maxWidth: 355
});
var i = 0;
for (i < 0; i < stops.length; i++) {
marker = new google.maps.Marker({
position:stops[i],
map: map,
title: "Stop Coords: "+ stops[i]
});
var len=stops.length;
popupDirections(marker, len, stops);
}
}
function popupDirections(marker, len,stops) {
//this function created listener listens for click on a marker
google.maps.event.addListener(marker, 'click', function () {
for (var i = 0; i < len; i++)
{ infowindow.setContent("Stop coords: " + stops[i]); } //sets the content of your global infowindow to string "Tests: "
infowindow.open(map, marker); //then opens the infowindow at the marker
});
}
Now, when I hover the mouse over each marker, the correct lat/lng is shown, but when I click on the marker and the infowindow pops up, I keep getting the lat/lng of stops[2]. I am not sure what the problem is, my knowledge of JS is pretty limited, I was fortunate to find a helpful post earlier on here that showed how to create infowindows.