您可以通過使用文本搜索做到這一點: -
當你搜索「倫敦」
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";
}
}
查詢結果中包含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"
}
因此,如果您搜索的名字最好是正確的地方,如果你使用的是谷歌地圖用於該地方的名稱,那麼只會有一個結果。例如:當您搜索「倫敦,英國」它會給結果只有一個:
{
"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"
}
我發現它更容易通過XML請求,並使用PHP的SimpleXML函數解析。 – zak
喲可以做到這一點。對不起,最初你的問題感到困惑,我以爲你有地方ID,是的感謝關於PHP的參考。在php中已經有一段時間了。無論如何,你仍然需要小心一點,就是所有地方的名字。很高興知道你能解決問題。 –