Google Place API Details Without Map

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.

Read More

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

Related posts

Leave a Reply