javascript - Google Map API v2 - GeoCoder - 如何自定义标记?

标签 javascript google-maps asynchronous geocoding

我尝试使用这种方式(示例)使用 Gmap V2 添加/自定义标记:

for (var i in table)
{
    var myvar = table[i]['text'] ;
    var myaddress = table[i]['address'] ;

    geocoder.getLatLng(
            myaddress , 
            function(point) {
                alert(myvar) ; // here myvar is wrong
                // ... adding customer markers ...            
            } 
    });
}

在这个示例中,我得到了表中每个条目的优点,但是 myvar 是错误的,在每次调用中:myvar 保持等于表的最后一个条目...

geocoder.getLatLng 是异步函数,是因为那个吗?


编辑: 谢谢你的回复。但是当我使用循环时,我遇到了这个问题,例如:

var address = 'somewhere'; 
for (i = 0 ; i < 3 ; i++) 
{
    geocoder.getLatLng(
            address,
            function(point) {
                if (point) {
                    alert(i); 
                }
    });
}

点总是等于 3 !

最佳答案

这就是我所做的。

        if ($('#map_canvas').length != 0) {

            var marker= new GIcon(title);
            marker.image = '/images/icon.png';
            marker.iconSize = new GSize(139,64);
            marker.iconAnchor = new GPoint(0, 64);
            marker.name = title ;


              markerOptions = { icon:marker };
             map = new GMap2(document.getElementById("map_canvas"));
             geocoder = new GClientGeocoder();
             geocoder.getLatLng(
              address,
              function(point) {
                if (!point) {
                  alert(address + " not found");
                } else {
                  map.setCenter(point, 14);
                  map.addOverlay(new GMarker(point, markerOptions));
                  }
                }
              );      
        }
    });

如果这对您没有帮助,您可以查看: http://code.google.com/intl/da/apis/maps/documentation/javascript/overlays.html#Icons

关于javascript - Google Map API v2 - GeoCoder - 如何自定义标记?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4040750/

相关文章:

javascript - 如何使用Gulp编译子目录中的JS?

javascript - 将值传递给新日期,给出无效日期

javascript - 检测网站中的注入(inject)内容

javascript - 在 Angular 中创建 "tags"列表

c++ - 如何处理异步函数中的异常 UWP App GetFileFromPathAsync(path);

javascript - 点击 Google map 标记时添加信息窗口气泡

javascript - 如何使用 fitBounds() 覆盖 Google map 初始化的选项;

asp.net - 将数据库中的多个纬度/经度点加载到谷歌地图标记中的好方法?

c# - 从多个线程等待相同的任务是否可以 - 等待线程安全吗?

ios - 如何在iOS中干净利落地封装并依次执行一系列后台任务?