Google Maps V3地理编码和循环中的标记 [英] Google Maps V3 geocoding and markers in loop

查看:138
本文介绍了Google Maps V3地理编码和循环中的标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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

它使用分割函数保存在temp中,并使用for循环将它们1乘1。

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

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

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.

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

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

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

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


推荐答案

这里是 JSFiddle Demo 使用虚拟地址并提醒显示与每个标记关联的正确数据:

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

什么你在for循环中是一个典型的闭包/范围问题。要解决这个问题,在传递到 temp [i] 变量之前,使用closure来定位

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
    }

这篇关于Google Maps V3地理编码和循环中的标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆