简体   繁体   中英

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. 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:

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

Then create the autocomplete object and call 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:

autoComplete.getPlace();

I've searched the Google Places Javascript API and there's mention of the statuses

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.

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?

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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