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

发布于 2024-09-30 01:44:25 字数 905 浏览 2 评论 0原文

我尝试使用这种方式(示例)使用 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!

I try to add/customize markers with Gmap V2 by using this way (example) :

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

In this sample I got the good point for each entry in the table, but myvar is wrong, in each calls : myvar stay equal to the last entry of the table...

geocoder.getLatLng is asynchronous function, is due to that ?


Edit:
Thanks for you reply. But I just have that issue when I use a loop for example :

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

Point always equal to 3 !

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

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

发布评论

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

评论(2

凉月流沐 2024-10-07 01:44:25

这就是我所做的。

        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

here is what i do.

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

If this is not helping you can take a look at:
http://code.google.com/intl/da/apis/maps/documentation/javascript/overlays.html#Icons

水溶 2024-10-07 01:44:25

自动解决:

geocoder = new GClientGeocoder();
map = new GMap2(document.getElementById("map"));
map.setCenter(new GLatLng(50, 3, 13));

geocoder = new GClientGeocoder();
function showAddress(address) 
{
    var adresses = ["10 place de la joliette, 13002 MARSEILLE",
    "15 place de la joliette, 13002 MARSEILLE",
    "20 place de la joliette, 13002 MARSEILLE"];

    for (var i = 0; i < adresses.length; i++) 
    {
        alert(adresses[i]);
        var address = adresses[i];
        addMarkerAtGeocode(address);
    }
}
function addMarkerAtGeocode(address) 
{
    geocoder.getLatLng(address, function(point) {
            if (!point) {
                alert(address + " not found");
            } else {
                alert('2:' +   address);
                var marker = createMarker(point, address);
                map.addOverlay(marker);
            }
    });
}

function createMarker(point, address) 
{
    var marker = new GMarker(point);
    return marker;
}

showAddress() ;

Autosolved :

geocoder = new GClientGeocoder();
map = new GMap2(document.getElementById("map"));
map.setCenter(new GLatLng(50, 3, 13));

geocoder = new GClientGeocoder();
function showAddress(address) 
{
    var adresses = ["10 place de la joliette, 13002 MARSEILLE",
    "15 place de la joliette, 13002 MARSEILLE",
    "20 place de la joliette, 13002 MARSEILLE"];

    for (var i = 0; i < adresses.length; i++) 
    {
        alert(adresses[i]);
        var address = adresses[i];
        addMarkerAtGeocode(address);
    }
}
function addMarkerAtGeocode(address) 
{
    geocoder.getLatLng(address, function(point) {
            if (!point) {
                alert(address + " not found");
            } else {
                alert('2:' +   address);
                var marker = createMarker(point, address);
                map.addOverlay(marker);
            }
    });
}

function createMarker(point, address) 
{
    var marker = new GMarker(point);
    return marker;
}

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