我想在谷歌地图信息窗口中应用jquery我该怎么办?

时间:2018-01-29 19:18:45

标签: javascript jquery html google-maps google-maps-api-3

当我点击每个标记信息窗口时,我有一个谷歌地图画布有多个标记来我想在谷歌地图信息窗口中使用jquery但是它没有用,我试过这样的东西。我我正在学习jquery希望你们中的任何人都能得到答案,谢谢

<html>
<head>

  <title>Google Maps Multiple Markers</title>
  <script src="http://maps.google.com/maps/api/js?sensor=false" type="text/javascript"></script>
  <script type="text/javascript"
    src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("#infowindowDiv").mouseover(function(){
    alert("hiii")

    })

});
</script>   
</head>
<body>
  <div id="map" style="height: 400px; width: 500px;">
</div>
<script type="text/javascript">
    var locations = [
      ['Bondi Beach', -33.890542, 151.274856, 4],
      ['Coogee Beach', -33.923036, 151.259052, 5],
      ['Cronulla Beach', -34.028249, 151.157507, 3],
      ['Manly Beach', -33.80010128657071, 151.28747820854187, 2],
      ['Maroubra Beach', -33.950198, 151.259302, 1]
    ];

    var map = new google.maps.Map(document.getElementById('map'), {
      zoom: 10,
      center: new google.maps.LatLng(-33.92, 151.25),
      mapTypeId: google.maps.MapTypeId.ROADMAP
    });

    var infowindow = new google.maps.InfoWindow();

    var marker, i;

    for (i = 0; i < locations.length; i++) { 
      marker = new google.maps.Marker({
        position: new google.maps.LatLng(locations[i][1], locations[i][2]),
        map: map
      });
       var html = '<div id="infowindowDiv">hii</div>'
      google.maps.event.addListener(marker, 'click', (function(marker, i) {
        return function() {
          infowindow.setContent(html);
          infowindow.open(map, marker);
        }
      })(marker, i));

    }
  </script>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

  1. 将InfoWindow内容添加到DOM后运行您的jquery(当InfoWindow的domready事件触发时:
  2. google.maps.event.addListener(infowindow, 'domready', function() {
      $(".infowindowDiv").mouseover(function() {
        alert("hiii")
      })
    });
    
    1. infowindowDiv类添加到InfoWIndow中的DIV:
    2. var html = '<div id="infowindowDiv" class="infowindowDiv">hii</div>'
      

      proof of concept fiddle

      代码段

      var locations = [
        ['Bondi Beach', -33.890542, 151.274856, 4],
        ['Coogee Beach', -33.923036, 151.259052, 5],
        ['Cronulla Beach', -34.028249, 151.157507, 3],
        ['Manly Beach', -33.80010128657071, 151.28747820854187, 2],
        ['Maroubra Beach', -33.950198, 151.259302, 1]
      ];
      
      function initialize() {
        var map = new google.maps.Map(document.getElementById('map'), {
          zoom: 10,
          center: new google.maps.LatLng(-33.92, 151.25),
          mapTypeId: google.maps.MapTypeId.ROADMAP
        });
      
        var infowindow = new google.maps.InfoWindow();
      
        var marker, i;
      
        for (i = 0; i < locations.length; i++) {
          marker = new google.maps.Marker({
            position: new google.maps.LatLng(locations[i][1], locations[i][2]),
            map: map
          });
          var html = '<div id="infowindowDiv" class="infowindowDiv">hii</div>'
          google.maps.event.addListener(marker, 'click', (function(marker, i) {
            return function() {
              infowindow.setContent(html);
              infowindow.open(map, marker);
              google.maps.event.addListener(infowindow, 'domready', function() {
                $(".infowindowDiv").mouseover(function() {
                  alert("hiii")
                })
              })
            }
          })(marker, i));
      
        }
      }
      google.maps.event.addDomListener(window, "load", initialize);
      html,
      body,
      #map {
        height: 100%;
        width: 100%;
        margin: 0px;
        padding: 0px
      }
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
      <script src="https://maps.googleapis.com/maps/api/js"></script>
      <div id="map"></div>