首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在打开新的InfoWindow时关闭每个InfoWindow

在打开新的InfoWindow时关闭每个InfoWindow
EN

Stack Overflow用户
提问于 2012-10-16 11:05:17
回答 1查看 312关注 0票数 1

经过大量的研究,我似乎不能确定为什么在打开一个新窗口时无法关闭InfoWindow。到目前为止,我尝试过的所有操作都导致InfoWindow根本不显示。

我在这里尝试过建议:Google Map API v3 ~ Simply Close an infowindow?和其他地方,但我想知道是不是一个我没有意识到的问题导致了这个问题。诚然,这是从其他来源拼凑而成的。如果您能帮忙,我将不胜感激。

代码语言:javascript
复制
    <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>
EN

回答 1

Stack Overflow用户

发布于 2013-08-24 07:40:02

您只能使用一个infowindow对象并动态设置不同的内容

示例: var infowindow =新的google.maps.InfoWindow({maxWidth: 200});

将侦听器添加到您的标记

代码语言:javascript
复制
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());
});
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12906958

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档