Infowindow使用谷歌地图

时间:2011-05-09 14:06:15

标签: javascript google-maps

我一直在使用Google地图测试一些内容,虽然这段代码为我提供了带有标记的地图,但我没有在点击上获得叠加层。我实际上正在使用Rails,但这是我的Rails视图正在生成的代码 - 但我的javascript知识非常少。

<script type="text/javascript"> 
  function initialize() {
    var latlng = new google.maps.LatLng(38.92226, -77.02515);
    var myOptions = {
      zoom: 15,
      center: latlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };

    var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

    var contentString = '1X438A40';
    var infowindow = new google.maps.InfoWindow({
          content: contentString
      });

    var marker = new google.maps.Marker({
              position: latlng,
              map: map,
              title:"1X438A40"
    });
  }

  google.maps.event.addListener(marker, 'click', function() {
    infowindow.open(map,marker);
  });

</script> 

1 个答案:

答案 0 :(得分:1)

你的addListener函数超出了initialize()函数的范围。将它放在大括号内应该可以解决问题。

    <script type="text/javascript"> 
      function initialize() {
        var latlng = new google.maps.LatLng(38.92226, -77.02515);
        var myOptions = {
          zoom: 15,
          center: latlng,
          mapTypeId: google.maps.MapTypeId.ROADMAP
        };

        var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

        var contentString = '1X438A40';
        var infowindow = new google.maps.InfoWindow({
              content: contentString
          });

        var marker = new google.maps.Marker({
                  position: latlng,
                  map: map,
                  title:"1X438A40"
        });

      google.maps.event.addListener(marker, 'click', function() {
        infowindow.open(map,marker);
      });
    }

    </script>