Google 地图 V3 地理编码和循环标记

发布于 2024-10-21 10:33:24 字数 1135 浏览 3 评论 0原文

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

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

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

它通过 split 函数保存在“temp”中,并通过 for 循环将它们获取为 1 1

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

这是页面,数据库中的最后一个条目是 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')});
     });  
};

I have some problems with my code, I have a list of airports in a sql database, and I want to create markers for each 1 of those airports.

For the address i got ICAO-codes for each airport, an ICAO is unique for each airport

I get the data from the Database as array

it's is saved in "temp" with an split function and with the for-loop it get them 1 by 1

Geocoding is not the problem, but I don't know why for the TITLE and the on click event
it is always the last one from the array which is used.

here is the page, the last entry in the database is ZBAA.

And all the markers are placed at the correct location but the title is wrong :s

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

The problem is with "address" i think but i m not sure.

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')});
     });  
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一绘本一梦想 2024-10-28 10:33:24

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

您拥有的是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
    }

Here is a JSFiddle Demo using "dummy" addresses and alert to show the correct data associate with each marker:

What you have is a typical closure/scope issue within the for loop. To fix the issue use closure to localize the temp[i] variable before passing into geocode and callback function within it:

    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
    }
星星的轨迹 2024-10-28 10:33:24

我的猜测是因为

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 如果我没有记错的话,某些浏览器会拒绝弹出窗口标题(并可能导致无法打开弹出窗口)。我总是留空。

my guess is because

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

callback is execute in same context.

try execute marker in same context. the code below is will wait for all geocoder fetched. then parse to marker.

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 if i'm not mistaken, some browser reject popup title(and might cause unable to open popup). i alway leave blank.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文