Search code examples
google-mapsgoogle-maps-api-3google-places-apigoogle-placesgoogle-places-autocomplete

What is the best practice for saving and refreshing a place_id from google places api?


On google places documentation it says:

"Best practise is to refresh your stored place IDs periodically, after 100 days for example. One strategy is to store the original request that returned each place ID. If a place ID becomes invalid, you can re-issue that request to get fresh results. These results may or may not include the original place."

I'm using AutocompleteService.getPlacePredictions to retrieve places and save the place_id for the selected place.

Which other field should i save for later "refreshing" of the place_id and what is the best method of "refreshing" a single place_id? (I assume not using the AutocompleteService)


Solution

  • As said in the Documentation posted in your question you can store the request that returned the place_id. So one way would be saving the user input (text used to do the request to Autocomplete) for the giving place_id.

    Another way would be saving the "description" field (in the predictions) for the giving place_id. As stated in the documentation "description is the matched prediction" and since is associated with the place_id. You can be sure that this field is associated with the place.

    It seems that saving the description will work but in some (very specific) cases they also might be changed in the database, giving inaccurate results.

    Saving input text (used in autocomplete request) is better suggested but this one needs to be a strong query. what I mean by this is:

    for example if you want to save the place_id for Times Square, maybe with the input "times" you already get a predictions for it, but "times" itself is not a strong query, so better save "times square".

    And make sure the query you saved always get you your desired place as first prediction.