开发者

Infowindow with Google maps

开发者 https://www.devze.com 2023-03-04 12:22 出处:网络
I\'ve been testing some stuff with Google maps, and while this code gives me a map with a marker, I\'m not getting the overlay on the click. I\'m actually using Rails, but this is the code my Rails vi

I've been testing some stuff with Google maps, and while this code gives me a map with a marker, I'm not getting the overlay on the click. I'm actually using Rails, but this is the code my Rails view is producing -- but my javascript knowledge is very minimal.

<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> 


Your addListener function is out of the initialize() functions' scope. Getting it inside the curly brackets should do the trick.

    <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>
0

精彩评论

暂无评论...
验证码 换一张
取 消