【问题标题】:Google Maps V3 geocoding and markers in loop循环中的 Google Maps V3 地理编码和标记
【发布时间】:2011-07-14 14:31:54
【问题描述】:

我的代码有一些问题,我在 sql 数据库中有一个机场列表,我想为这些机场中的每一个创建标记。

对于我获得每个机场的 ICAO 代码的地址,每个机场的 ICAO 都是唯一的

我从数据库中获取数据作为数组

它通过拆分函数保存在“temp”中,并通过for循环将它们一一对应

地理编码不是问题,但我不知道为什么 TITLE 和点击事件 它始终是使用的数组中的最后一个。

这里是页面,数据库中的最后一个条目是ZBAA。

所有标记都放置在正确的位置,但标题是错误的:s

http://mizar.lte.lu/~pr1011_meteo/projet/cartemonde4.php

我认为问题在于“地址”,但我不确定。

for (var i = 0; i < temp.length; ++i){

     var address=temp[i];

     geocoder.geocode({ 'address': address}, function(results){            
          var marker  = new google.maps.Marker({
              map: map, 
              position: results[0].geometry.location,
              title:address
          });

          google.maps.event.addListener(marker, 'click', function() {
               window.open ('infomonde.php?icao='+address+'&language=fr', 'Informations météo', config='height=400, width=850, toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, directories=no, status=no')});
     });  
};

【问题讨论】:

  • 它必须与你传递给address=temp[i] 的 i 一起做,好像你需要做一个闭包并传入 address 但如果不放一个 jsfiddle 演示,我不能确定
  • 你介意为地址/临时数组提供一些虚拟字段吗?
  • 关闭是怎么回事?你对虚拟字段是什么意思?如果您的意思是我已经尝试过输入一些错误的条目并且地理编码失败^^
  • @user657848 查看我的示例并探索演示。如果您有任何问题,请告诉我,并随时发表评论

标签: javascript google-maps google-maps-api-3 closures geocoding


【解决方案1】:

这是一个JSFiddle Demo,使用“虚拟”地址和警报来显示与每个标记关联的正确数据:

您所拥有的是 for 循环中的典型闭包/范围问题。要解决此问题,请使用闭包对 temp[i] 变量进行本地化,然后再将其传递给其中的地理编码和回调函数:

    for (var i = 0; i < temp.length; ++i) {
        (function(address) {
            geocoder.geocode({
                'address': address
            }, function(results) {
                var marker = new google.maps.Marker({
                    map: map,
                    position: results[0].geometry.location,
                    title: address
                });

                google.maps.event.addListener(marker, 'click', function() {
                    //alert(address);  //use alert to debug address
                    window.open('infomonde.php?icao=' + address + '&language=fr', 'Informations météo', config = 'height=400, width=850, toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, directories=no, status=no')
                });
            });
        })(temp[i]);  //closure passing in temp[i] and use address within the closure
    }

【讨论】:

    【解决方案2】:

    我猜是因为

    geocoder.geocode({ 'address': address}, function(results){ ... });
    

    回调在相同的上下文中执行。

    尝试在相同的上下文中执行标记。下面的代码将等待获取所有地理编码器。然后解析为标记。

    var results = {};
    var waiting = temp.length;
    
    while(temp.length > 0){
    
      var fetching = temp.pop();
    
      geocoder.geocode(
        { address: fetching}, 
        function(response){
          results[fetching] = response[0].geometry.location;
          --waiting;
          if(waiting == 0) // wait for everything to finish
            setMarker();
        }
      );
    }
    var setMarker = function(){
      for(var element in results){
        var marker  = new google.maps.Marker({
                  map: map, 
                  position: results[element],
                  title: element
                  });
    
        google.maps.event.addListener(marker, 'click', function() {
        window.open ('infomonde.php?icao='+element+'&language=fr', '', 'configs')});
      }
    }
    

    ps window.open 如果我没记错的话,某些浏览器会拒绝弹出标题(并可能导致无法打开弹出窗口)。我总是留空。

    【讨论】:

    • 所以我应该保留 address=temp[i] 吗?因为您在地理编码中使用了它
    • {address:address}改成{address:fetching}我的错
    猜你喜欢
    • 2013-12-18
    • 2013-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多