2012-10-16 125 views
1

經過大量研究後,我似乎無法確定爲什麼在打開新窗口時無法關閉InfoWindow。我迄今爲止試過的所有東西都會導致InfoWindow根本無法顯示。關閉每個InfoWindow打開新的InfoWindow

我在這裏試過建議:Google Map API v3 ~ Simply Close an infowindow?和其他地方,但我想知道如果這是一個問題,我不知道這是造成這個問題。無可否認,這是從其他來源拼湊而成的。我非常感謝你的幫助。

<html> 
    <head> 

    <meta name="viewport" content="initial-scale=1.0, user-scalable=no"> 
    <meta charset="utf-8"> 
    <title>Google Maps JavaScript API v3 Example: Geocoding Simple</title> 
    <link href="https://code.googleapis.com/maps/documentation/javascript/examples/default.css" rel="stylesheet"> 
    <script src="https://maps.googleapis.com/maps/api/js?sensor=false"></script> 
    <script> 
    var geocoder; 
    var map; 
    var infoWindow; 


    function initialize() { 
    geocoder = new google.maps.Geocoder(); 
    var latlng = new google.maps.LatLng(37.77014301036805, -79.48862973916635); 
    var mapOptions = { 
     zoom: 12, 
     center: latlng, 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
    } 
    map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions); 

    } 

    function codeAddress() { 

    var address = document.getElementById("address").value; 
    geocoder.geocode({ 'address': address}, function(results, status) { 
     if (status == google.maps.GeocoderStatus.OK) { 
     map.setCenter(results[0].geometry.location); 
     var loc=[]; // no need to define it in outer function now 
     loc[0]=results[0].geometry.location.lat(); 
     loc[1]=results[0].geometry.location.lng(); 
       var content = '<div id="infoWindow"><iframe src="http://inside.handp.com/map2.php?'; 
     content += 'latitude=' + results[0].geometry.location.lat()+ '&'; 
     content += 'longitude=' + results[0].geometry.location.lng()+'" frameborder="0" allowfullscreen scrolling="no"></iframe></div>'; 


//  display(loc[0]); 
     var marker = new google.maps.Marker({ 
      map: map, 
      position: results[0].geometry.location 


     }); 

     addInfoWindow(marker, content); 

     } else { 
     alert("Geocode was not successful for the following reason: " + status); 
     } 
    }); 
    } 

    function display(long_lat){ 
    alert(long_lat); 
    } 
    function addInfoWindow(marker, message) { 
      var info = message; 

      var infoWindow = new google.maps.InfoWindow({content: message}); 

      google.maps.event.addListener(marker, 'click', function() { 

      }); 

       infoWindow.open(map, marker); 

     } 

    </script> 
    </head> 
<body onload="initialize()"> 
<div id="map_canvas" style="width: 640px; height: 480px;"></div> 
    <div> 
    <input id="address" type="textbox" value="Enter Address"> 
    <input type="button" value="Encode" onclick="codeAddress()"> 
    </div> 
</body> 
</html> 

回答

2

可以只使用一個信息窗口對象和動態設置不同的內容

例如: VAR信息窗口=新google.maps.InfoWindow({maxWidth:200});

添加監聽到你的標記物

var marker = new google.maps.Marker({ 
    map: map, 
    info: 'this is info window html', 
    position: results[0].geometry.location 
}); 

google.maps.event.addListener(marker, 'click', function() {    
    infowindow.setContent(this.info); 
    infowindow.open(map, this); 
    map.setCenter(this.getPosition()); 
});