我遵循一個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}');
});
任何幫助表示讚賞。謝謝
是的 - 它做到了。非常感謝! –
作爲附錄,地圖初始化時是否可以打開其中一個信息窗口? –