Google Geocoder 异步问题

发布于 2024-11-09 20:53:25 字数 1524 浏览 4 评论 0原文

我相信我在地理编码器结果方面遇到了时间问题。请参阅下面的代码片段。

我基本上是在执行地理编码并获取结果。然后,我通过 jQuery AJAX 调用将结果传递给服务器端方法。最后该方法的结果返回一个 JSON 对象。根据结果​​,我可能会也可能不会执行第二次地理编码。这就是问题所在。

您可以看到我有一个变量 hasResult 默认为 false,并在确定有效结果时切换为 true。在所有情况下,这都是完美的,除非需要第二次地理编码。地理编码成功发生并且代码执行,但最终的hasResult 检查仍然返回 false。怎么会这样呢?

var hasResult = false;            
geocoder.geocode({ "address": address }, function (results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
        $.ajax({
            url: "URL",
            type: "POST",
            dataType: "json",
            data: results
            contentType: "application/json; charset=utf-8",
            success: function (result) {
                        if (result.Valid) {
                            hasResult = false;
                            //Some other code, works perfect                            
                        } else {
                            geocoder.geocode({ "address": result.ValidRequest }, function (resultsNew, statusNew) {
                                if (statusNew == google.maps.GeocoderStatus.OK) {
                                    hasResult = false; 
                                    //Some other code, works perfect
                                }
                            });
                        }
                    });
                });
            }
        });
    }
});

if (hasResult == true) {
    alert('Success');
} else {
    alert('Fail');
}

谢谢

I believe that I have having a timing issue with geocoder results. See a snippet of code below.

I am basically performing a geocode and getting the result. I then pass the result to a server side method via the jQuery AJAX call. Finally the result of the method returns a JSON object. Based on the result I may or may not perform a second geocode. This is where the problem lies.

You can see I have a variable hasResult defaulted to false, and is toggled to true when a valid result has been determined. In all cases this works perfect, except when the need for a second geocode occurs. The geocode happens successfully and the code executes, but the final hasResult check still returns false. How can this be?

var hasResult = false;            
geocoder.geocode({ "address": address }, function (results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
        $.ajax({
            url: "URL",
            type: "POST",
            dataType: "json",
            data: results
            contentType: "application/json; charset=utf-8",
            success: function (result) {
                        if (result.Valid) {
                            hasResult = false;
                            //Some other code, works perfect                            
                        } else {
                            geocoder.geocode({ "address": result.ValidRequest }, function (resultsNew, statusNew) {
                                if (statusNew == google.maps.GeocoderStatus.OK) {
                                    hasResult = false; 
                                    //Some other code, works perfect
                                }
                            });
                        }
                    });
                });
            }
        });
    }
});

if (hasResult == true) {
    alert('Success');
} else {
    alert('Fail');
}

Thanks

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

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

发布评论

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

评论(1

终止放荡 2024-11-16 20:53:26

这是因为您在从服务器获取结果之前先检查了 hasResult 的值。发生这种情况是因为请求是异步的。您应该改用回调。

编辑:

尝试一下(这只是一个模型,未经测试,更改它以提高可读性,将 successOrFailure 作为参数/回调等传递):

var hasResult = false;            

var successOrFailure = function(hasResult){
    if (hasResult == true) {
        alert('Success');
    } else {
        alert('Fail');
    }
};

geocoder.geocode({ "address": address }, function (results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
        $.ajax({
            url: "URL",
            type: "POST",
            dataType: "json",
            data: results
            contentType: "application/json; charset=utf-8",
            success: function (result) {
                        if (result.Valid) {
                            hasResult = false;
                            successOrFailure(hasResult);
                            //Some other code, works perfect                            
                        } else {
                            geocoder.geocode({ "address": result.ValidRequest }, function (resultsNew, statusNew) {
                                if (statusNew == google.maps.GeocoderStatus.OK) {
                                    hasResult = false;
                                    successOrFailure(hasResult); 
                                    //Some other code, works perfect
                                }
                            });
                        }
                    });
                });
            }
        });
    }
});

This is because you check the value of hasResult before you get the results from the server. This happens, because the request is asynchrounous. You should use callbacks instead.

Edit:

Try this (this is only a mockup and is not tested, change it to improve readability, pass successOrFailure as a parameter/callback etc.):

var hasResult = false;            

var successOrFailure = function(hasResult){
    if (hasResult == true) {
        alert('Success');
    } else {
        alert('Fail');
    }
};

geocoder.geocode({ "address": address }, function (results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
        $.ajax({
            url: "URL",
            type: "POST",
            dataType: "json",
            data: results
            contentType: "application/json; charset=utf-8",
            success: function (result) {
                        if (result.Valid) {
                            hasResult = false;
                            successOrFailure(hasResult);
                            //Some other code, works perfect                            
                        } else {
                            geocoder.geocode({ "address": result.ValidRequest }, function (resultsNew, statusNew) {
                                if (statusNew == google.maps.GeocoderStatus.OK) {
                                    hasResult = false;
                                    successOrFailure(hasResult); 
                                    //Some other code, works perfect
                                }
                            });
                        }
                    });
                });
            }
        });
    }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文