OverlappingMarkerSpiderfier

OverlappingMarkerSpiderfier

因此,我是Google Maps的初学者,我一直在查看OverlappingMarkerSpiderfier Github的说明,但是经过几个小时的摆弄,我什么都做不了!我的代码非常简单,因为我正处于学习过程中。这是代码:

<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <title>Simple markers</title>
    <style>
      html, body, #map-canvas {
        height: 100%;
        margin: 0px;
        padding: 0px
      }
      </style>
      <script src="http://jawj.github.io/OverlappingMarkerSpiderfier/bin/oms.min.js"></script> //added the file
    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp"></script>
    <script>

    function initialize() {
      var myLatlng = new google.maps.LatLng(-25.363882,131.044922);
      var mapOptions = {
        zoom: 4,
        center: myLatlng
      }
      var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
      var oms = new OverlappingMarkerSpiderfier(map); //Spiderfied it here
      var iw = new gm.InfoWindow();
      oms.addListener('click', function(marker, event) {
        iw.setContent(marker.desc);
        iw.open(map, marker);
      });
      oms.addListener('spiderfy', function(markers) {
        iw.close();
      });

      var locations = ["Sydney, Australia", "Sydney, Australia", "Sydney, Australia", "Sydney, Australia"]

      for (var i = 0; i < locations.length; i ++) {
        var title_i = "title " + i;
        var desc_i = "description " + i;
        var loc = new gm.LatLng(-35.3080, 149.1245);
        var marker = new gm.Marker({
          position: loc,
          title: title_i,
          map: map
        });
        marker.desc = desc_i;
        oms.addMarker(marker);  // <-- here attempted to add markers
      }
    }

    google.maps.event.addDomListener(window, 'load', initialize);

    </script>
  </head>
  <body>
    <div id="map-canvas"></div>
  </body>
</html>


它与Github上的代码非常相似,但是不幸的是,我仍然无法使它正常工作。我从搞乱地理编码开始,将其与可自定义的标记(例如不同的颜色标记)集成在一起,但我只是无法运行Open Spiderfier。

最佳答案

有两个问题:


您必须先加载maps-API,然后再加载oms.min.js
您已省略以下部分:var gm=google.maps;。将其放在initialize()的开头

关于javascript - 谷歌 map :OverlappingMarkerSpiderfier,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24317693/

10-10 23:30