I'm following a SitePoint tutorial for integrating the Google Maps API into our site with jQuery, and I've got everything working really well, with one exception: each new marker opens a separate info window, without closing the previous one. I'm trying to figure out how to have only one window open at a time.
Here's the tutorial in question: http://www.sitepoint.com/google-maps-api-jquery/
I've checked this question here: Have just one InfoWindow open in Google Maps API v3 but I wasn't able to solve my problem by following the answer there (I could have easily misinterpreted).
My code looks like this:
$(document).ready(function(){
var MYMAP = {
map: null,
bounds: null
}
MYMAP.init = function(selector, latLng, zoom) {
var myOptions = {
zoom:zoom,
center: latLng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
this.map = new google.maps.Map($(selector)[0], myOptions);
this.bounds = new google.maps.LatLngBounds();
}
MYMAP.placeMarkers = function(filename) {
$.get(filename, function(json){
$.each(json, function(i,loc){
var marker = new google.maps.Marker({
position: new google.maps.LatLng(loc.location.merchant_location.latitude, loc.location.merchant_location.longitude),
map: MYMAP.map,
title: loc.deal.subject
});
var arrMarkers = [];
arrMarkers[i] = marker;
var infoWindow = new google.maps.InfoWindow({
content: "<h3>" + loc.deal.subject + "</h3><p>" + loc.location.merchant_location.address + "</p>"
});
var arrInfoWindows = [];
arrInfoWindows[i] = infoWindow;
google.maps.event.addListener(marker, 'click', function(){
infoWindow.open(MYMAP.map,marker);
});
});
}, "json");
}
$("#map").css({
height: 500,
width: 600
});
var myLatLng = new google.maps.LatLng(#{@deals.first.merchant_locations.first.latitude},#{@deals.first.merchant_locations.first.longitude});
MYMAP.init('#map', myLatLng, 11);
MYMAP.placeMarkers('/more_deals/get_locations/#{@market.id}');
});
Any help is appreciated. Thanks