如何请求Google Place API返回某个地点的ID?

时间:2014-12-27 13:14:48

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

我有一个PHP变量

$location = 'London';

我还有Google Maps API v3 javascript来显示我的地图和放置在地点ID上的标记

 var request = {
    placeId: 'ChIJN1t_tDeuEmsRUsoyG83frY4'
 };

我的目标是让我的脚本请求我的PHP变量的地方ID并将ID打印到我的标记位置,如下所示:

 var request = {
    placeID: '<?=$_locationID;?>'
 };

如何将$ location的地点ID作为$ _locationID的值传递?

2 个答案:

答案 0 :(得分:1)

您可以使用textsearch执行此操作: -

function geoPlaceID($location){
   $locationclean = str_replace (" ", "+", $location);
   $details_url = "https://maps.googleapis.com/maps/api/place/textsearch/json?query=" . $locationclean . "&key=YOUR_API_KEY";
   $du = file_get_contents($details_url);
   $getDetails = json_decode(utf8_encode($du),true);
   if ($getDetails['status']=="OK"){
       return $getDetails['results'][0]['place_id'];
   }
   else{
       return "Place ID not found";
   }
}
当您搜索“london”包含多于1个结果时

查询结果:

{
   "html_attributions" : [],
   "results" : [
      {
         "formatted_address" : "London, UK",
         "geometry" : {
            "location" : {
               "lat" : 51.5073509,
               "lng" : -0.1277583
            },
            "viewport" : {
               "northeast" : {
                  "lat" : 51.6723432,
                  "lng" : 0.148271
               },
               "southwest" : {
                  "lat" : 51.38494009999999,
                  "lng" : -0.3514683
               }
            }
         },
         "icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/geocode-71.png",
         "id" : "b1a8b96daab5065cf4a08f953e577c34cdf769c0",
         "name" : "London",
         "place_id" : "ChIJdd4hrwug2EcRmSrV3Vo6llI",
         "reference" : "CoQBdgAAAEaMNGBLSJDQzPro1sZAjnWPObrVhKk_U0-oQ9HDOhJG5Z0gUd6ZfkuvhIXOeNhs7KFRMC84dGTj_mLaXC9tXhZtluPztMwUFOTg99vDXpsccnh4iyhrYfJBZNMNOSzrLOUdXAfnBTb18EclrhWBmAbKQ8aLzJEtZyQOqc3i3FEYEhAGxbUwRj6GFekMSU19_8NaGhS03o6gl1eWpWdx5AmiKJ-oMKTRuQ",
         "types" : [ "locality", "political" ]
      },
      {
         "formatted_address" : "London, ON, Canada",
         "geometry" : {
            "location" : {
               "lat" : 42.9869502,
               "lng" : -81.243177
            },
            "viewport" : {
               "northeast" : {
                  "lat" : 43.073245,
                  "lng" : -81.1063879
               },
               "southwest" : {
                  "lat" : 42.824517,
                  "lng" : -81.390852
               }
            }
         },
         "icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/geocode-71.png",
         "id" : "fab6815aadba41e81aa7bd51a445c4924317ec88",
         "name" : "London",
         "place_id" : "ChIJC5uNqA7yLogRlWsFmmnXxyg",
         "reference" : "CoQBfgAAANqxMzFC52IV0GIn-ORiCKA5gKmt0L-CPyQJLC18UNjggnHMy0HkRN_BK5ylwViFGt_u6KsEKu9ZnhGySBrkDOdZdsFDHRJQezW1uQrBs9jc8zNF8QxlHuKh2CvncuNQbqM1kfqtpa0WXA6__0zarFTiaWff1SAJY-v4Ix9vaK0JEhCtT-q0QzngXMnALyPbp7qSGhSlejUY7ilJRTG4SfpeNRrKzZYksQ",
         "types" : [ "locality", "political" ]
      },
      {
         "formatted_address" : "London, KY, USA",
         "geometry" : {
            "location" : {
               "lat" : 37.1289771,
               "lng" : -84.08326459999999
            },
            "viewport" : {
               "northeast" : {
                  "lat" : 37.1522599,
                  "lng" : -84.03595709999999
               },
               "southwest" : {
                  "lat" : 37.0797589,
                  "lng" : -84.126262
               }
            }
         },
         "icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/geocode-71.png",
         "id" : "692adb38e4db9759599d6f73f300a3bba7db714a",
         "name" : "London",
         "place_id" : "ChIJA8c2lxTNXIgRmMwHc-RRGWI",
         "reference" : "CoQBewAAAOQz_2KNfQsQ7ph5U5cUQKjAka2ckTsdzNhyvChJKeep9MRnv4Si4mo4xBHnNtC44-mToEuALwktcuQaoevRj-AVbBX6rvwUQHkY9KuiomTgocq1uBriE0FNk1m8PEhsOHFbVxpGB61tTL-aC7NLGyix-gpBk90wfvFL6WnMtEGbEhAn2hYFbt2MymyoD56sbNEJGhTZEFCIBDTbYIY4_LTbEXtb823GYg",
         "types" : [ "locality", "political" ]
      }
   ],
   "status" : "OK"
}

因此,如果您使用谷歌地图为该地点使用的地点的名称,那么如果您正确搜索该地点的名称会更好,那么只会有一个结果。示例:当您搜索“london,UK”时,它只会给出一个结果:

{
   "html_attributions" : [],
   "results" : [
      {
         "formatted_address" : "London, UK",
         "geometry" : {
            "location" : {
               "lat" : 51.5073509,
               "lng" : -0.1277583
            },
            "viewport" : {
               "northeast" : {
                  "lat" : 51.6723432,
                  "lng" : 0.148271
               },
               "southwest" : {
                  "lat" : 51.38494009999999,
                  "lng" : -0.3514683
               }
            }
         },
         "icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/geocode-71.png",
         "id" : "b1a8b96daab5065cf4a08f953e577c34cdf769c0",
         "name" : "London",
         "place_id" : "ChIJdd4hrwug2EcRmSrV3Vo6llI",
         "reference" : "CoQBdQAAAB9gHd2srTAOUumLaGYrpjppDaNOVEqRrMa58s9SADNLRXGPzLyEgLQ17zOuhJXa3ygKO-14zz6zKxNCwO24MS5zvP60EviITZlvou6exZBlIapBPcZNsFYHWlw7wTzYaj4jWqj1eZPO6FZJ2eyYeKCldZfQVT_4sMWAXmIdxklDEhCOnCuvdUriA6kyIjN4HJ6RGhQnnkzVDVFCui_lEGziSfPKmY1T7g",
         "types" : [ "locality", "political" ]
      }
   ],
   "status" : "OK"
}

答案 1 :(得分:0)

$data = "https://maps.googleapis.com/maps/api/place/textsearch/xml?query=".$location."&key=YOUR_KEY_HERE";

$xml = file_get_contents($data);
$xml = simplexml_load_string($xml);

foreach($xml->result->place_id as $key => $value){
    $location_id = $value;
}

使用此代码,我已为每个位置返回了正确的ID,在本例中我的$ location ='London'

使用Maps JavaScript,最终结果是

enter image description here