同步调用Google Geocoding API方法

发布于 2024-10-15 02:33:07 字数 1904 浏览 6 评论 0原文

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

基本上我有一个方法:

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

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

I need to know marker's address when I change markers postition.

Basically I have a method:

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

If I extract geocoding code into new method:

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

I have no luck because this call is asynchronous. And I need that new addres when I stop moving marker. Is there a solution to this one?

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

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

发布评论

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

评论(1

烟─花易冷 2024-10-22 02:33:07

原始发帖者的回答

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

ANSWER by Original Poster

[@vale4674 put this as an edit to their question. Copying here as an answer.]

@vale4674 replaced

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

with

setTitle(marker);

and added this method:

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