2010-05-14 41 views
0

我見過其他帖子,但他們沒有像我的動態循環標記。如何使用以下代碼創建一個關閉另一個標記時關閉infowindow的事件?如何使用Google Maps 3 API關閉infowindow?

$(function(){ 
    var latlng = new google.maps.LatLng(45.522015,-122.683811); 
    var settings = { 
     zoom: 10, 
     center: latlng, 
     disableDefaultUI:false, 
     mapTypeId: google.maps.MapTypeId.SATELLITE 
    }; 
    var map = new google.maps.Map(document.getElementById("map_canvas"), settings); 

    $.getJSON('api',function(json){ 
     for (var property in json) { 
      if (json.hasOwnProperty(property)) { 
       var json_data = json[property]; 
       var the_marker = new google.maps.Marker({ 
        title:json_data.item.headline, 
        map:map, 
        clickable:true, 
        position:new google.maps.LatLng(
         parseFloat(json_data.item.geoarray[0].latitude), 
         parseFloat(json_data.item.geoarray[0].longitude) 
        ) 
       }); 
       function buildHandler(map, marker, content) { 
        return function() { 
         var infowindow = new google.maps.InfoWindow({ 
          content: '<div class="marker"><h1>'+content.headline+'</h1><p>'+content.full_content+'</p></div>' 
         }); 
         infowindow.open(map, marker); 
        }; 
       } 
       new google.maps.event.addListener(the_marker, 'click',buildHandler(map, the_marker, {'headline':json_data.item.headline,'full_content':json_data.item.full_content})); 
      } 
     } 
    }); 
}); 

回答

0

我終於找到它了...沒有感謝任何人在這裏......這實際上是相當容易:

首先,設置一些瓦爾來存儲信息窗口:

var infowindow; 

然後將此添加到觸發其他infowindow.open()的onClick函數的任何位置。把它放在開放之上雖然:

if(infowindow) {infowindow.close()} 

在你的循環內或其他你正在添加的標記。

E.g.在全面的行動:

在我的腳本的頂部:

var infowindow; 

裏面我添加標記的循環:

function buildHandler(map, marker, content) { 
    return function() { 
     if(infowindow) {infowindow.close()} 
     infowindow = new google.maps.InfoWindow({ 
      content: 'My Content here' 
     }); 
     infowindow.open(map, marker); 
    }; 
}