2

Suppose a very simple function to query Query Autocomplete:

$query_google_places = function ($input, array $location = null) {
    $parameters = ['key' => 'AIzaSyAjCY7qYiaMv8Kkk_-D1Ha7wlRkr5neppk', 'input' => $input, 'sensor' => 'false'];

    if ($location) {
        $parameters['location'] = $location['lat'] . ',' . $location['lng'];
        $parameters['radius'] = 10000;
    }

    $url = 'https://maps.googleapis.com/maps/api/place/queryautocomplete/json?' . \http_build_query($parameters);

    $response = file_get_contents($url);

    return $response;
};

This returns:

{
   "predictions" : [
      {
         "description" : "Pizza Hut, Halifax, United Kingdom",
         "id" : "d9274717e1e203c41287fa5936701b9584814965",
         "matched_substrings" : [
            {
               "length" : 9,
               "offset" : 0
            }
         ],
         "reference" : "CmRaAAAAyulRkdspSJ2Mr5kmlhYOC-ZT7AsKdnnwjGkFFoGHNjMWwTS5mNvQBu6FqoQ89E4U0eC-PNGN0wPD8WEYgO1if6jMmxWAqkraDFOryC8B8cWTvX333l_0UaRv_sXBIITqEhCSrz9aQB0fch-AFDS3wKG7GhTRoQw09IODWJIorqjjwCYTktkfKw",
         "terms" : [
            {
               "offset" : 0,
               "value" : "Pizza Hut"
            },
            {
               "offset" : 11,
               "value" : "Halifax"
            },
            {
               "offset" : 20,
               "value" : "United Kingdom"
            }
         ],
         "types" : [ "establishment" ]
      },
      {
         "description" : "Pizza Hut, John William Street, Huddersfield, United Kingdom",
         "id" : "a1571b1434ac1491b5f6775d9a30f8ec7798a310",
         "matched_substrings" : [
            {
               "length" : 9,
               "offset" : 0
            }
         ],
         "reference" : "CoQBdAAAADaFqqtB9NGk_v1yZdY3m6OxILfLva556GBHxHIVdPHu_R0fQCm5kCduCjKL4BgiIdoNIejet3WI7xNanaiWDpiMD1Ml7Q9EnfhypReK0N6mQIY3TxOUcmf_INhkenU3ZVL8Vk9lAxqjq5ZfxiEZk9Vv8GaEnok_OJnaRBuZdCmtEhAYs-0Qc2Zza4fLuUXIiLknGhQogPaBcUm10Y59EA5i6BWPbTNXcA",
         "terms" : [
            {
               "offset" : 0,
               "value" : "Pizza Hut"
            },
            {
               "offset" : 11,
               "value" : "John William Street"
            },
            {
               "offset" : 32,
               "value" : "Huddersfield"
            },
            {
               "offset" : 46,
               "value" : "United Kingdom"
            }
         ],
         "types" : [ "establishment" ]
      },
      {
         "description" : "pizza hut",
         "matched_substrings" : [
            {
               "length" : 9,
               "offset" : 0
            }
         ],
         "terms" : [
            {
               "offset" : 0,
               "value" : "pizza hut"
            }
         ]
      },
      {
         "description" : "Pizza Hut Delivery, Wakefield Road, Huddersfield, United Kingdom",
         "id" : "0c6830de14a8e23bf971626e880201f5290ec9a6",
         "matched_substrings" : [
            {
               "length" : 9,
               "offset" : 0
            }
         ],
         "reference" : "CoQBeQAAAJnMKHhJh-IE64z6seh3sbnm4dt0h1zbMYLr4BPGCITQTZLp4lkm12HKEFw0Bt761UaZPsESrY4ha7XSXusqqRVstmRFBF2wiwF5HGMO9DhRSeIzJ8CkNvsxmlqINosGBBPUHEDQEeSOoPHn3u3MeByoLVw6AzZ9N6eTrm94hsX9EhBB9cIoXjhHjR9Tr3zBROM7GhRhpYagW9qWs9hGZ3V0o9WXfrfcXw",
         "terms" : [
            {
               "offset" : 0,
               "value" : "Pizza Hut Delivery"
            },
            {
               "offset" : 20,
               "value" : "Wakefield Road"
            },
            {
               "offset" : 36,
               "value" : "Huddersfield"
            },
            {
               "offset" : 50,
               "value" : "United Kingdom"
            }
         ],
         "types" : [ "establishment" ]
      },
      {
         "description" : "Pizza Hut Delivery, Westgate, Halifax, United Kingdom",
         "id" : "667c14a2cb5bb676bd02911e924db82fb5cbe6df",
         "matched_substrings" : [
            {
               "length" : 9,
               "offset" : 0
            }
         ],
         "reference" : "CnRtAAAAEtMYQ9N4Gmn8dpZqzHOxYGPCNP9QQ6M-gHjj2hJv2euH4gUVvDhmgazi6LcRjdYpUF41moGLr26IGc2vOPfvFg_kHTqTIYHHwiD84bu0PeEzmLzIzJw2IkGNpcW1o6weO86TW8zaUaMFZ2zNW-tynhIQFT8GvSn_q7DAoP3ytM14ExoUN3sShljpfaCWEUknvsfAjZJ_Ru4",
         "terms" : [
            {
               "offset" : 0,
               "value" : "Pizza Hut Delivery"
            },
            {
               "offset" : 20,
               "value" : "Westgate"
            },
            {
               "offset" : 30,
               "value" : "Halifax"
            },
            {
               "offset" : 39,
               "value" : "United Kingdom"
            }
         ],
         "types" : [ "establishment" ]
      }
   ],
   "status" : "OK"
}

How do I get coordinates for each result? Note, that this is server-side question.

Gajus
  • 69,002
  • 70
  • 275
  • 438

1 Answers1

0

IMO, autocompletion or query autocompletion results live in a different world than search results. Google assumes you're not supposed to need coordinates in the autocompletion list because this is not the data you will be presenting on your map or in any controller. The data you'll present will be the result of a true "search" request.

Conclusion: you're supposed to call again Google server on endpoints such as nearby search, text search or details to get the coordinates.

If you want to use details, if the user clicked on a prediction from autocomplete for example, you'll use reference attribute as a parameter of your http request to identify the clicked prediction.

Aurelien Porte
  • 2,692
  • 27
  • 32