Reputation: 151
I have a PHP variable
$location = 'London';
I also have Google Maps API v3 javascript to display my map and a marker placed on a place ID
var request = {
placeId: 'ChIJN1t_tDeuEmsRUsoyG83frY4'
};
My goal is to have my script request the place id of my PHP variable and print the ID into my marker location, like this:
var request = {
placeID: '<?=$_locationID;?>'
};
How can I pass the place ID of $location as the value of $_locationID?
Upvotes: 4
Views: 2476
Reputation: 151
$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;
}
With this code I have returned the correct ID for each location, in this instance my $location = 'London'
With Maps JavaScript the final result is
Upvotes: 1
Reputation: 1897
You can do it by using 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";
}
}
query result when you search "london" contains more than 1 result:
{
"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"
}
Therefore it is better if you search name of the place properly if you used the name of the place that is used by google maps for that place then there will be only one result. Example: When you search "london, UK" it will give only one result:
{
"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"
}
Upvotes: 2