Javascript Google Maps Api,带链接的多个标记,只有geoceder

时间:2016-09-07 14:43:51

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

我在链接到多个标记时遇到问题,我可以在地图上显示我的标记,但是当我尝试将链接放在它们上面时,我在所有标记上总是使用相同的链接,最后一个。在这里,我提供了一个示例代码:

getSingleScalarResult

任何解决方案? 提前致谢

1 个答案:

答案 0 :(得分:0)

由于异步代码,您需要稍微更改代码

function setMarkers() {
    markers.forEach(function(item) {
        var url = item.url;
        geocoder.geocode({'address': item.name}, function(results, status) {
        if (status === 'OK') {

            var marker = new google.maps.Marker({
                map: map,
                position: results[0].geometry.location,
                title: results[0].address_components[0].long_name,
            });
            google.maps.event.addListener(marker, "click", function() {
                window.location.href = url;
            });
        } else {
        /*console.log('Geocode was not successful for the following reason: ' + status);*/
        }});
    }
}