如何从查询自动完成中获取坐标?

时间:2013-12-02 18:19:49

标签: php google-maps google-maps-api-3

假设一个非常简单的函数来查询查询自动完成:

$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;
};

返回:

{
   "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"
}

如何获得每个结果的坐标?请注意,这是服务器端问题。

1 个答案:

答案 0 :(得分:0)

IMO,自动完成或查询自动填充结果与搜索结果存在于不同的世界中。 Google假定您不应该在自动完成列表中需要坐标,因为这不是您将在地图或任何控制器中显示的数据。您将呈现的数据将是真正“搜索”请求的结果。

结论:您应该在nearby searchtext searchdetails等终端上再次呼叫Google服务器以获取坐标。

如果您想使用详细信息,例如,如果用户点击自动填充中的预测,您将使用reference属性作为您的http请求的参数来识别所点击的预测。