我遵循一个SitePoint教程,将Google Maps API与jQuery集成到我们的网站中,并且我已经将所有工作都非常好,只有一个例外:每个新标记打开一个单独的信息窗口,而不关闭前一个。我试图找出如何一次只打开一个窗口。Google Maps API - 打开单个infoWindow
这里是有问题的教程:http://www.sitepoint.com/google-maps-api-jquery/
我在这里签了这个问题:Have just one InfoWindow open in Google Maps API v3,但我没能按照答案了(我可以很容易误解)来解决我的问题。
我的代码如下所示:
$(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}');
});
任何帮助表示赞赏。谢谢
是的 - 它做到了。非常感谢! –
作为附录,地图初始化时是否可以打开其中一个信息窗口? –