[google maps API] 關閉前一個 infowindow

  1. var currentInfoWindow = ''//Global variable   
  2.   
  3. google.maps.event.addListener(marker, 'click'function()   
  4.    {   
  5.       if(currentInfoWindow != '')   
  6.       {    
  7.         currentInfoWindow.close();   
  8.         currentInfoWindow = '';   
  9.       }   
  10.       var infoWindow = new google.maps.InfoWindow({content: 'COntent'});   
  11.       infowindow.open(map, marker);   
  12.       currentInfoWindow = infowindow;   
  13. });  

  1. // Initialize infowindow   
  2. var infowindow = new google.maps.InfoWindow({content: ''});   
  3.   
  4. function add_marker(point, name, content)   
  5. {   
  6.    var marker = new google.maps.Marker({   
  7.       map: map,   
  8.       position: point,   
  9.       dragable: false,   
  10.       clickable: true,   
  11.       name: name   
  12.    });   
  13.    marker.content = content;   
  14.    google.maps.event.addListener(marker, 'click'function()   
  15.    {   
  16.       infowindow.content = marker.content;   
  17.       infowindow.open(map, marker);   
  18.    });   
  19.    return marker;   
  20. };   
  21.   
  22. function addPostCode(zip, html)    
  23. {   
  24.    geocoder.geocode( { 'address': zip}, function(results, status)    
  25.    {   
  26.       if (status == google.maps.GeocoderStatus.OK)   
  27.       {   
  28.          map.setCenter(results[0].geometry.location);   
  29.          var marker = add_marker(results[0].geometry.location, zip, html)   
  30.       });   
  31. });  

很常會使用到的。

出處

留言