Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the unique identifier for a city through the Google Places Autocomplete API performing the search in multiple languages​​?

I need to get a unique identifier for the name of a city when that same city is again sought in another language API to find this place same city.

Example:

I do a search via the Google API Autocomplete place like this:

City: New York

The result is:

New York, NY, USA Country code: U.S.

And when I do another search for "New York" in Russian, for example, I need a unique identifier for New York to know that New York is the same in English as in Russian "Нью-Йорк"

The results is:

Нью-Йорк, Нью-Йорк, США Код страны: США

My question is how do I identify that "New York City" is the same in both searches, as I compare the results from the same city in different language and identify the two answers refer to the same New York City?

like image 534
Panda Avatar asked Mar 14 '13 23:03

Panda


1 Answers

Consider the following two queries.

https://maps.googleapis.com/maps/api/place/autocomplete/json?input=newyork&types=(cities)&language=en&sensor=true&key=<YOUR-API-KEY>

Here are few first lines of the response in JSON. As you see there is an id available in the result (reference values are redacted):

{
   "predictions" : [
      {
         "description" : "New York, NY, United States",
         "id" : "7eae6a016a9c6f58e2044573fb8f14227b6e1f96",
         "matched_substrings" : [
            {
               "length" : 8,
               "offset" : 0
            }
         ],
         "reference" : "CkQzAAAAs[...]g5OK5Y",
         ...

Now the query for Russian:

https://maps.googleapis.com/maps/api/place/autocomplete/json?input=Нью-Йорк&types=(cities)&language=ru&sensor=true&key=<YOUR-API-KEY>

This query returns few more predictions (cities with the same name in Missouri, Nebraska, etc.), but still you can find New York City from the Russian results using the id:

    {
   "predictions" : [
      {
         "description" : "Нью-Йорк, Соединенные Штаты Америки",
         "id" : "7eae6a016a9c6f58e2044573fb8f14227b6e1f96",
         "matched_substrings" : [
            {
               "length" : 8,
               "offset" : 0
            }
         ],
         "reference" : "CmRZAAAA[...]s10LLA",
         ... 

      {
         "description" : "Нью Йорк, Миссури, Соединенные Штаты Америки",
         "id" : "211cae70217210a77581fd2a2dca45bc4286e064",
         ...
         "reference" : "CnRpAAAA[...]FUydA",
         ...
      {
         "description" : "Нью Йорк, Небраска, Соединенные Штаты Америки",
         "id" : "10a97689b3441120410e18c71d7f1b9ed70ac2e7",
         ...
         "reference" : "CnRrAAAA[...]XrkzYY",
         ...
      {
         "description" : "Нью-Йорк Милс, Нью-Йорк, Соединенные Штаты Америки",
         "id" : "2902d4dc0e1ecfe52aef4da6d5eb06e4c9fab4d9",
         ...
         "reference" : "CoQBcwAAA[...]g3G1Jw",
         ...

And you can query the Place Details API using the reference (which is different for each of the prediction results above):

https://maps.googleapis.com/maps/api/place/details/json?reference=CkRAAAAA[...]cLKfOc_4mvY&language=ru&sensor=true&key=<YOUR-API-KEY>

And use id to verify the identity of the place you are searching for. Quoting Place Details API documentation:

id contains a unique stable identifier denoting this place. This identifier may not be used to retrieve information about this place, but can be used to consolidate data about this Place, and to verify the identity of a Place across separate searches. As ids can occasionally change, it's recommended that the stored id for a Place be compared with the id returned in later Details requests for the same Place, and updated if necessary.

Passing id to this query is still possible, but reference is a required field:

https://maps.googleapis.com/maps/api/place/details/json?id=7eae6a016a9c6f58e2044573fb8f14227b6e1f96&language=ru&reference=CkRAAAAA[...]cLKfOc_4mvY&sensor=true&key=<YOUR-API-KEY>
like image 119
Shervin Avatar answered Oct 19 '22 20:10

Shervin