2011-10-17 58 views
0

我遵循一個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}'); 

});

任何幫助表示讚賞。謝謝

回答

3

您正在您的.each()循環中創建infowindow。相反,使用該循環創建一個infowindow。然後在你的事件監聽器中,每次更新全局infowindow的內容。

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){ 
    var infoWindow = new google.maps.InfoWindow({ 
      content: "" 
     }); 

    $.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 
     }); 

     bindInfoWindow(marker, MYMAP.map, infoWindow, "<h3>" + loc.deal.subject + "</h3><p>" + loc.location.merchant_location.address + "</p>"); 
    });   
    }, "json"); 
} 

function bindInfoWindow(marker, map, infowindow, html) { 
    google.maps.event.addListener(marker, 'click', function() { 
     infowindow.setContent(html); 
     infowindow.open(map, marker); 
    }); 
} 

$("#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}'); 
+0

是的 - 它做到了。非常感謝! –

+0

作爲附錄,地圖初始化時是否可以打開其中一個信息窗口? –

0

只創建一個InfoWindow對象。 您的修改代碼。

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) { 

var infoWindow = new google.maps.InfoWindow(); 

    $.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; 


     google.maps.event.addListener(marker, 'click', function(){ 
     infoWindow.setContent (
     "<h3>" + loc.deal.subject + "</h3><p>" + loc.location.merchant_location.address + "</p>" 
    ); 
     infoWindow.open(MYMAP.map,marker); 
     }); 
    });   
    }, "json"); 
} 

$("#map").css({ 
    height: 500, 
    width: 600 
}); 
相關問題