Community Forums › Forums › Archived Forums › General Discussion › How To Use Google Places API
Tagged: api, developers, google, javascript, maps, places
- This topic has 7 replies, 3 voices, and was last updated 9 years, 6 months ago by Porter.
-
AuthorPosts
-
May 9, 2015 at 7:06 pm #151357PorterParticipant
I don't really need instructions on the specifics of the API itself, I more or less need to know how to get it up and running on the syntax side within a page template. Key things I don't get:
-Syntax of mixing javascript with PHP
-If I enque the script in functions.php, how do I reference it within my template?
-Anything else I may need to know to use this for getting data to be used with my PHP in the template.I may just be fried from working / reading too much about this, but I seem to be stuck and have no experience with mixing javascript and php, so any help would be appreciated.
https://developers.google.com/maps/documentation/javascript/places
May 9, 2015 at 11:58 pm #151396upthinkParticipantPhp executes on the server - while Javascript runs on the client. So basically use the server side language to echo statements which will run as javascript on the browser. For example:
<?php echo "<script> jQuery(document).ready(function($) { ///whatever you want }); </script>"; ?>
So basically when the HTML is sent to the browser the javascript code is in there to run on the browser.
I think any enqueue that you do in functions.php should be available in templates - so you should not bother about that. BUT it will increase the load time (so better to enqueue only inside the template where you need it).
Hope this helps
May 10, 2015 at 9:33 am #151485PorterParticipantThanks for the info, very useful. I actually wrapped my enqueue in a conditional tag for only that template, I just didn't want to complicate the question by expressing that 🙂
I guess my only confusion still is how the external file differs from say, a theme/js/somefile.js. The file is loaded from Google, so I don't actually have a .js file, just js in the template. Does that change anything? "document" and including jquery confused me a bit.
May 10, 2015 at 3:43 pm #151590PorterParticipantI'll make it a bit more clear, as I'm clearly very lost on this haha. This is what I have:
In functions.php:
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?
You mentioned it needing JQuery, which I know of it, but not exactly what it is. If it IS needed, would I change the enqueue to list it as a dependency in the third parameter? Lastly, I still don't get how to mix PHP with this script entirely. Like, once I have the "place" object, how do I save those values for use in PHP within my page template?
Sorry for all of my confusion - my background is a game programmer, so I'm great with logic, but lost with syntax and complexities like this. I'm quite out of my comfort zone to say the least, but I need to learn it, so I'm ready 🙂
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
May 10, 2015 at 7:26 pm #151612coralseaitMemberI'd suggest getting Yoast Local SEO, which integrates KMLs, Mapping, Local SEO, etc - our reasoning behind this is generally we'd rather pay the agency fee and have a supported solution for clients than roll our own (coming from years of enterprise support, we just don't want to deal with self supported solutions as much as possible).
May 10, 2015 at 7:36 pm #151613PorterParticipantI've considered grabbing it, but I don't know much about what it offers, and I really only need the hours of locations, as the upkeep on them changing (especially in Vermont), is a pain - $70 is pretty steep for just that. My website is best described as a local directory style site, but not in the usual sense. I'm writing personalized reviews on every bar, restaurant, hotel, etc, have an event system with all local events (music, dining specials, etc), and more. I'm using a custom post type of venues, with the Advanced Custom Fields plugin to record various other data, and have a custom template to serve all of this information.
I'd love it if I could install the plugin on a trial basis, but sadly I don't think that exists. Keep in mind what I'm doing is just for me, no clients, just the one site for me.
May 10, 2015 at 7:41 pm #151615coralseaitMemberGood deal, rolling your own seems to make great sense in this usage scenario - so you're on the right track. If you are responsible to supporting clients we'd say buy the plugin but for you own site you have plenty of time / bandwidth to sort it yourself and good learning chance so go for it!
May 10, 2015 at 7:43 pm #151616PorterParticipantThanks for the feedback 🙂
I use Yoast SEO, and read his articles regularly, so I'd definitely agree with you if I was working with a client. The plugin may still be of use to me (depending if I can apply the "improve local SEO" aspect to every business I cater to, though I have no idea if that can be done, and how that would be achieved (if it would conflict with my structure, etc).
I'll get to the bottom of figuring this out, it's definitely a skill I need to know!
-
AuthorPosts
- The forum ‘General Discussion’ is closed to new topics and replies.