2011-10-17 59 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 
}); 
相关问题