gpt4 book ai didi

javascript - Google Places Javascript API 获取响应状态

转载 作者:行者123 更新时间:2023-12-02 10:43:31 25 4
gpt4 key购买 nike

我已经了解了在使用 JSON 或 XML 版本的 Google Places API 响应时获取 API 状态的方法。主要示例显示尝试显示 map :

new google.maps.places.PlacesService(map);
service = new google.maps.places.PlacesService(map);
service.nearbySearch(request, callback);

function callback(results, status) {
if (status == google.maps.places.PlacesServiceStatus.OK) {
for (var i = 0; i < results.length; i++) {
var place = results[i];
createMarker(results[i]);
}
}
}

但是要让 Google 在 javascript 中放置自动完成字段,您可以使用 js 请求 url:

https://maps.googleapis.com/maps/api/js?key=MY_API_KEY;libraries=places&amp;callback=initAutocomplete

然后创建自动完成对象并调用 getPlace():

var autoComplete = new google.maps.places.Autocomplete(<html input element>);
autoComplete.getPlace();

但是,我没有看到任何状态选项嵌入在返回的 javascript 对象中:

autoComplete.getPlace();

我搜索了 Google Places Javascript API,其中提到了状态

The PlacesServiceStatus response object contains the status of the request, and may contain debugging information to help you track down why the Place Details request failed. Possible status values are:

ERROR: There was a problem contacting the Google servers.

INVALID_REQUEST: This request was invalid.

OK: The response contains a valid result.

OVER_QUERY_LIMIT: The webpage has gone over its request quota.

NOT_FOUND The referenced location was not found in the Places database.

REQUEST_DENIED: The webpage is not allowed to use the PlacesService.

UNKNOWN_ERROR: The PlacesService request could not be processed due to a server error. The request may succeed if you try again.

ZERO_RESULTS: No result was found for this request.

我真的希望在返回任何这些状态的情况下进行错误处理。这是使用 Javascript API 的限制吗?

最佳答案

这可能有点不太优雅,但是到目前为止还没有找到更好的答案,我目前正在使用这个:

if (google.maps.places.PlacesServiceStatus.OK === "OK") {

// my code here

}

关于javascript - Google Places Javascript API 获取响应状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38773437/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com