【问题标题】:I am trying to apply jquery in google map info windows How can I do it?我正在尝试在谷歌地图信息窗口中应用 jquery 我该怎么做?
【发布时间】:2018-01-29 19:18:45
【问题描述】:

我有一个谷歌地图画布,当我点击每个标记信息窗口时,它有多个标记你们中的任何人都会有答案,谢谢

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

【问题讨论】:

  • 在 InfoWindow 打开之前,DOM 中不存在 infowindowDiv。
  • 有什么解决办法吗?
  • InfoWindow 内容中也没有带有class="infowindowDiv" 的 div。
  • 对不起,我正在检查班级并保留相同的代码

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


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

proof of concept fiddle

代码 sn-p:

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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-10
    • 2013-05-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多