I’m looking to use the Google Places API within a page template with WordPress. I know I need to use the Javascript version, but that’s about it. Every tutorial I’ve found mentions map markers, and the code looks quite complex. I simply want to use the API to retrieve the information for use with my PHP within my template.
Aside from enqueueing the script in WordPress, I don’t really get how to use the API to simply retrieve the information. I know that’s kind of a broad request, but I can’t find any information on it anywhere. I’ve skimmed over the docs, but I either don’t understand it, or it doesn’t exactly apply. Any help would be greatly appreciated.
Update
Here’s what I have thus far:
add_action(âwp_enqueue_scriptsâ, âadd_scriptsâ)
function add_scripts() {
if (is_singular(âvenuesâ)) {
wp_enqueue_script(âgoogle-placesâ, âgoogle-mapsâ, âhttps://maps.googleapis.com/maps/api/js?libraries=placesâ);
}
}
In my template file:
<script>
var request = {
//placeId: 'ChIJN1t_tDeuEmsRUsoyG83frY4'
placeId: <?php $get_field('google_place_id'); ?> //Can I use php here like this (syntax) to get my dynamic place ID?
};
//service = new google.maps.places.PlacesService(map);
service = new google.maps.places.PlacesService($attrib[0]);
service.getDetails(request, callback);
function callback(place, status) {
if (status == google.maps.places.PlacesServiceStatus.OK) {
//Do something here to store information for use in PHP in this template.
}
}
</script>
Iâm giving the script the name âgoogle_placesâ, but I donât know where I actually use that given the example code I found. In the second bit for the service variable, I replaced the map with $attrib[0], as someone on stack exchange said that you could use a ânodeâ instead of a map, and I donât have a map. From what I understand of the callback function, âplaceâ is the returned object in which I can access all of the properties from, such as place.formatted_address?
The sources Iâm following right now are:
https://developers.google.com/maps/documentation/javascript/places#place_details
https://developers.google.com/maps/documentation/javascript/reference#PlaceResult