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 ansys fluent计算闪退
  • ¥15 有关wireshark抓包的问题
  • ¥15 需要写计算过程,不要写代码,求解答,数据都在图上
  • ¥15 向数据表用newid方式插入GUID问题
  • ¥15 multisim电路设计
  • ¥20 用keil,写代码解决两个问题,用库函数
  • ¥50 ID中开关量采样信号通道、以及程序流程的设计
  • ¥15 U-Mamba/nnunetv2固定随机数种子
  • ¥15 vba使用jmail发送邮件正文里面怎么加图片
  • ¥15 vb6.0如何向数据库中添加自动生成的字段数据。