Google地图+ javascript仅在编码警报时正确呈现

时间:2014-05-21 11:33:04

标签: javascript google-maps google-maps-api-3 geolocation

我使用这个例子:

https://developers.google.com/maps/documentation/javascript/examples/icon-complex

根据数据的数组变量呈现多标记谷歌地图。

这是我的代码:

geocoder = new google.maps.Geocoder();

var locations = [
    ["Title1", "Ballyblack Road, Portaferry, United Kingdom", 0, "/images/pins/1.png"],
    ["Title2", "Coach Road, Portaferry, United Kingdom", 1, "/images/pins/2.png"],
    ["Title3", "Cook Street, Portaferry, United Kingdom", 2, "/images/pins/2.png"],
    ["Title4", "Ballyfounder Road, Portaferry, United Kingdom", 3, "/images/pins/1.png"],
    ["Title5", "Shore Road, Strangford, United Kingdom", 4, "/images/pins/3.png"],
    ["Title6", "Cook Street, Portaferry, United Kingdom", 5, "/images/pins/1.png"],
    ["Title7", "Windmill Hill, Portaferry, United Kingdom", 6, "/images/pins/4.png"],
    ["Title8", "BT32", 7, "/images/pins/TownlandsandGateways.png"],
    ["Title9", "Shore Road, Strangford, United Kingdom", 8, "/images/pins/4.png"],
    ["Title10", "Windmill Lane, Portaferry, United Kingdom", 9, "/images/pins/1.png"],
    ["Title11", "The Saltpans, Portaferry, United Kingdom", 10, "/images/pins/3.png"],
    ["Title12", "Cuan View, Portaferry, United Kingdom", 11, "/images/pins/4.png"],
    ["Title13", "Lough Shore Road, Portaferry, United Kingdom", 12, "/images/pins/3.png"],
    ["Title14", "Castleward Road, Down, United Kingdom", 13, "/images/pins/1.png"],
    ["Title15", "Bar Hall Road, Portaferry, United Kingdom", 14, "/images/pins/2.png"],
    ["Title16", "Bar Hall Road, Portaferry, United Kingdom", 15, "/images/pins/1.png"]
];

function initialize() {
    var mapOptions = {center: new google.maps.LatLng(54.380357, -5.542774), zoom: 14};
    var map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
    setMarkers(map, locations);
}

function setMarkers(map, locations) {
    for (var i = 0; i < locations.length; i++) {

        var location = locations[i];

        geocoder.geocode({'address': location[1]}, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                markers = new google.maps.Marker({
                    position: results[0].geometry.location,
                    map: map,
                    icon: location[3],
                    title: location[0],
                    zIndex: location[2]
                });
            }
        });
// alert(location[1]);
    }
}
google.maps.event.addDomListener(window, 'load', initialize); 

阵列参数是[&#34; Title&#34;,&#34; Location&#34;,&#34; zIndex&#34;,&#34; Image&#34;]

您会看到我已经将警报贴近底部。当我运行这个代码时它会渲染所有16个引脚,但它们都是引脚16 - 尽管它确实为它们找到了正确的位置,但是标记图像都是相同的并且标题都是相同的。

当我取消注释警报时,点击&#34;确定&#34;在警告框中,标记都正确呈现,具有自己的标题和自己的图标。

我正在挠头,想知道如何为每个找到正确的位置而不是正确的标题或图标图像......!

我的猜测是代码正在快速运行,以便地图找到正确的值...但我的直觉是说......真的???太快了???

我最终计划从大型数据库构建数组,因此可能会有100个标记。

关于我可能做错的任何建议?

非常感谢。

1 个答案:

答案 0 :(得分:1)

之所以发生这种情况,是因为您不等待地理编码器响应,在地理编码返回响应之前更改变量位置。 你应该有这样的东西:

function setMarkers(map, locations) {
    for (var i = 0; i < locations.length; i++) {

        setMarker( map, locations[i] );

    }
}


function setMarker( map, location ) {
    geocoder.geocode({'address': location[1]}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            markers = new google.maps.Marker({
                position: results[0].geometry.location,
                map: map,
                icon: location[3],
                title: location[0],
                zIndex: location[2]
            });
        }
    });
}

通过这种方式,每个标记都会有一个不同的局部变量位置