【问题标题】:Call Google Geocoding API method synchronouslly同步调用 Google Geocoding API 方法
【发布时间】:2011-06-18 22:01:41
【问题描述】:

当我改变标记位置时,我需要知道标记的地址。

基本上我有一个方法:

function addNewMarker(latLng) {
    geocoder.geocode({'latLng': latLng}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            if (results[0]) {
                var address = results[0].formatted_address;
                var marker = new google.maps.Marker({
                    position: latLng, 
                    map: map, 
                    draggable: true,
                    title: address,
                }); 


                google.maps.event.addListener(marker, 'dragend', function() {
                    //marker.setTitle(getGeocodeResults(marker.getPosition()));
                    marker.setTitle(***THIS IS NEW ADDRESS OF THIS MARKER***);
                });
            }
        } else {
            alert("Geocoder failed due to: " + status);
        }
    });
}

如果我将地理编码代码提取到新方法中:

function addNewMarker(latLng){
    var address = getGeocodeResults(latLng);
    var marker = new google.maps.Marker({
        position: latLng, 
        map: map, 
        draggable: true,
        title: address,
    }); 


    google.maps.event.addListener(marker, 'dragend', function() {
        marker.setTitle(getGeocodeResults(marker.getPosition()));
    });
}

function getGeocodeResults(latLng){
    geocoder.geocode({'latLng': latLng}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            if (results[0]) {
                var address = results[0].formatted_address;
                return address;
            }
        } else {
            alert("Geocoder failed due to: " + status);
        }
    });
}

我没有运气,因为这个调用是异步的。当我停止移动标记时,我需要新的地址。 有解决办法吗?

【问题讨论】:

    标签: google-maps google-maps-api-3 geocoding synchronous


    【解决方案1】:

    原始海报的回答

    [@vale4674 将此作为对他们问题的修改。复制到这里作为答案。]

    @vale4674 被替换

    marker.setTitle(getGeocodeResults(marker.getPosition()));
    

    setTitle(marker);
    

    并添加了这个方法:

    function setTitle(marker){
        geocoder.geocode({'latLng': marker.getPosition()}, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                if (results[0]) {
                    var address = results[0].formatted_address;
                    marker.setTitle(address);
                }
            } else {
                alert("Geocoder failed due to: " + status);
            }
        });
    }
    

    【讨论】:

    • 我现在不明白。我是否必须在我的问题中进行编辑,或者您是否已经使用此答案进行了编辑?不知道流程。
    • 您已将此答案作为对您问题的编辑。但是,答案应该是答案,而不是问题的一部分。所以我把它从问题中删除并放在这里作为答案。
    • 更多信息:你可以接受这个答案(你可能知道,它会给我 15 点声望点,给你 2 点声望点)。或者,如果您不想将代表点数给实际上没有提出答案的人,您可以自己发布答案,然后接受您自己的答案。 (接受自己的答案是完全可以的,尽管您可能需要等待一段时间才能这样做。此外,您不会因为接受自己的答案而获得任何声誉积分。)
    • 我认为我无法回答我的问题。这是新事物吗?
    • 据我所知,回答您自己的问题一直很好,但我可能错了,也许它是相对较新的。无论如何,在stackoverflow.com/faq 的常见问题解答中提到:“问和回答你自己的问题也很好......”
    猜你喜欢
    • 2012-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-27
    • 2016-04-07
    • 1970-01-01
    相关资源
    最近更新 更多