简体   繁体   English

Google Places Javascript API 获取响应状态

[英]Google Places Javascript API get the response status

I've seen ways to get the API status when using the JSON or XML version of the Google Places API responses.我已经看到了在使用 JSON 或 XML 版本的 Google Places API 响应时获取 API 状态的方法。 Mainly the examples show trying to display a 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]);
    }
  }
}

But to just have a Google places autocomplete field in javascript you use the js request url:但是要让 Google 在 javascript 中放置自动完成字段,您可以使用 js 请求 url:

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

Then create the autocomplete object and call getPlace():然后创建自动完成对象并调用 getPlace():

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

However, I don't see any status option embedded in the javascript object returned by:但是,我没有看到在返回的 javascript 对象中嵌入任何状态选项:

autoComplete.getPlace();

I've searched the Google Places Javascript API and there's mention of the statuses我搜索了 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. PlacesServiceStatus 响应对象包含请求的状态,并且可能包含调试信息以帮助您追踪地点详细信息请求失败的原因。 Possible status values are:可能的状态值为:

ERROR: There was a problem contacting the Google servers.错误:联系 Google 服务器时出现问题。

INVALID_REQUEST: This request was invalid. INVALID_REQUEST:此请求无效。

OK: The response contains a valid result. OK:响应包含有效结果。

OVER_QUERY_LIMIT: The webpage has gone over its request quota. OVER_QUERY_LIMIT:网页已超过其请求配额。

NOT_FOUND The referenced location was not found in the Places database. NOT_FOUND 在 Places 数据库中找不到引用的位置。

REQUEST_DENIED: The webpage is not allowed to use the PlacesService. REQUEST_DENIED:网页不允许使用 PlacesService。

UNKNOWN_ERROR: The PlacesService request could not be processed due to a server error. UNKNOWN_ERROR:由于服务器错误,无法处理 PlacesService 请求。 The request may succeed if you try again.如果您再试一次,请求可能会成功。

ZERO_RESULTS: No result was found for this request. ZERO_RESULTS:未找到此请求的结果。

I'd really like to have error handling in place in the case of any of these statuses being returned.我真的很想在返回任何这些状态的情况下进行错误处理。 Is this a limitation of using the Javascript API?这是使用 Javascript API 的限制吗?

This may be somewhat less than elegant but, having not so far found a better answer, I am currently using this:这可能不太优雅,但到目前为止还没有找到更好的答案,我目前正在使用它:

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

        // my code here

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM