Google 地图 V3 地理编码和循环标记
我的代码有一些问题,我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个 JSFiddle 演示,使用“虚拟”地址和警报来显示与每个标记关联的正确数据:
您拥有的是for 循环中典型的闭包/范围问题。要解决此问题,请在传递到地理编码和其中的回调函数之前使用闭包本地化
temp[i]
变量: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:我的猜测是因为
回调是在相同的上下文中执行的。
尝试在相同的上下文中执行标记。下面的代码将等待获取所有地理编码器。然后解析为标记。
ps window.open 如果我没有记错的话,某些浏览器会拒绝弹出窗口标题(并可能导致无法打开弹出窗口)。我总是留空。
my guess is because
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.
ps window.open if i'm not mistaken, some browser reject popup title(and might cause unable to open popup). i alway leave blank.