douhanzhuo6905 2014-05-30 16:04 采纳率: 100%
浏览 46
已采纳

Google Maps API v3 - 在一个页面上创建带有标记的多个地图

I'm trying to create a slider with 3 slides, each containing a mini map with marker pointing to certain location. I managed to create one slide, but every attempt to create more than one on this page results in failure. Here is my code so far:

HTML + PHP

<?php if (have_rows('job_offers')): ?>
  <?php
    $rows = get_field('job_offers');
    $row_count = count($rows);
      for ($i = 0; $i <= 2; $i++) {
  ?>
  <li>
  <div id='googleMap<?php echo $i; ?>' style="position:absolute;top:0;right:0;width:180px;height:100%;"></div>    
  </li>
  <?php } ?>
<?php endif; ?>    

and jQuery

var geocoder = new google.maps.Geocoder();
var map;
var locations = ["London",'Paris','Barcelona'];
var pos;

function initialize()
{
    google.maps.visualRefresh = true;
    getGeoCode();
}

function getGeoCode()
{
    for (var i=0; i<3 ; i++) {
    geocoder.geocode({'address': locations[i]}, function(results, status) {
        if (status === google.maps.GeocoderStatus.OK)
        {

            pos[i] = results[0].geometry.location;
            var mapOptions = {
                zoom: 8,
                center: pos[i],
                mapTypeId: google.maps.MapTypeId.ROADMAP,
                disableDefaultUI: true,
                styles: [
                    { stylers: [{hue:'#C4268C'},{lightness:-60},{saturation:100}]}
                ]
            };
            map[i] = new google.maps.Map(document.getElementById("googleMap" + i), mapOptions);
            var image = '<?php echo get_template_directory_uri(); ?>/images/marker.png';
            var marker = new google.maps.Marker({
                position: pos[i],
                icon:image
            });

            marker.setMap(map[i]);

        }
        else
        {
            alert("Not found");
        }
    });
}
}
google.maps.event.addDomListener(window, 'load', initialize);

When I didn't try to use arrays, everything was fine. I'll be grateful for any hints. Cheers, E.

  • 写回答

1条回答 默认 最新

  • douluan8828 2014-05-30 17:50
    关注

    The code below fixes the problem with function closure (them makeMap function holds closure on the value of "i" for use when the geocoder callback runs):

    var geocoder = new google.maps.Geocoder();
    var map = [];
    var locations = ["London",'Paris','Barcelona'];
    var pos = [];
    var infowindow = new google.maps.InfoWindow({});
    
    function initialize()
    {
        google.maps.visualRefresh = true;
        getGeoCode();
    }
    
    function makeMap(i) {
        geocoder.geocode({'address': locations[i]}, function(results, status) {
            if (status === google.maps.GeocoderStatus.OK)
            {
    
                pos[i] = results[0].geometry.location;
                var mapOptions = {
                    zoom: 8,
                    center: pos[i],
                    mapTypeId: google.maps.MapTypeId.ROADMAP,
                    disableDefaultUI: true,
                    styles: [
                        { stylers: [{hue:'#C4268C'},{lightness:-60},{saturation:100}]}
                    ]
                };
                map[i] = new google.maps.Map(document.getElementById("googleMap" + i), mapOptions);
                // var image = '<?php echo get_template_directory_uri(); ?>/images/marker.png';
                var marker = new google.maps.Marker({
                    position: pos[i],
                    // icon:image
                });
    
                marker.setMap(map[i]);
    
            }
            else
            {
                alert("Not found");
            }
        });
    }
    function getGeoCode()
    {
        for (var i=0; i<3 ; i++) {
            makeMap(i);
    }
    }
    google.maps.event.addDomListener(window, 'load', initialize);
    

    Working fiddle

    code snippet:

    var geocoder = new google.maps.Geocoder();
    var map = [];
    var locations = ["London", 'Paris', 'Barcelona'];
    var pos = [];
    var infowindow = new google.maps.InfoWindow({});
    
    function initialize() {
      google.maps.visualRefresh = true;
      getGeoCode();
    }
    
    function makeMap(i) {
      geocoder.geocode({
        'address': locations[i]
      }, function(results, status) {
        if (status === google.maps.GeocoderStatus.OK) {
    
          pos[i] = results[0].geometry.location;
          var mapOptions = {
            zoom: 8,
            center: pos[i],
            mapTypeId: google.maps.MapTypeId.ROADMAP,
            disableDefaultUI: true,
            styles: [{
              stylers: [{
                hue: '#C4268C'
              }, {
                lightness: -60
              }, {
                saturation: 100
              }]
            }]
          };
          map[i] = new google.maps.Map(document.getElementById("googleMap" + i), mapOptions);
          // var image = '<?php echo get_template_directory_uri(); ?>/images/marker.png';
          var marker = new google.maps.Marker({
            position: pos[i],
            // icon:image
          });
    
          marker.setMap(map[i]);
    
        } else {
          alert("Not found");
        }
      });
    }
    
    function getGeoCode() {
      for (var i = 0; i < 3; i++) {
        makeMap(i);
      }
    }
    google.maps.event.addDomListener(window, 'load', initialize);
    #googleMap0,
    #googleMap1,
    #googleMap2,
    #googleMap3 {
      width: 500px;
      height: 500px;
    }
    <script src="http://maps.google.com/maps/api/js"></script>
    <table border="1">
      <tr>
        <td>
          <div id="googleMap0"></div>
        </td>
      </tr>
      <tr>
        <td>
          <div id="googleMap1"></div>
        </td>
      </tr>
      <tr>
        <td>
          <div id="googleMap2"></div>
        </td>
      </tr>
    </table>

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 学不会递归,理解不了汉诺塔参数变化
  • ¥15 基于图神经网络的COVID-19药物筛选研究
  • ¥30 软件自定义无线电该怎样使用
  • ¥15 R语言mediation包做中介分析,直接效应和间接效应都很小,为什么?
  • ¥15 Jenkins+k8s部署slave节点offline
  • ¥15 如何实现从tello无人机上获取实时传输的视频流,然后将获取的视频通过yolov5进行检测
  • ¥15 WPF使用Canvas绘制矢量图问题
  • ¥15 用三极管设计一个单管共射放大电路
  • ¥15 孟德尔随机化r语言运行问题
  • ¥15 pyinstaller编译的时候出现No module named 'imp'