Can’t find a way to load my script after Google Maps API
-
Hi,
Here I’m stuck again… I need to pass by a manual installation of GoogleMap in my single template file (I know it exists but we don’t want to use the plugin for our case). So I paste the followed script (with my key) :
<script async defer src="https://maps.googleapis.com/maps/api/js?key=hidden_key&callback=initMap"></script>
I pasted it directly in DOM. If I enqueue the google map API script with the wp_enqueue_script function, the script doesn’t seem to be recognized anymore…
Now I need to get some Markers parameters in custom fields of some posts returned by a WP_query request after that. So I presume I should use the wp_localize_script function.
But it need an handle script. So the script which initialize the map and the markers should be external. I’m wrong until that ?<script async defer src="https://maps.googleapis.com/maps/api/js?key=AIzaSyAhE_CVUWwIOzXs-zD-vFdtgWBFWRBhQVY&callback=initMap"></script> <?php wp_enqueue_script( 'gmap_metierville_script', get_stylesheet_directory_uri() . '/assets/js/google_map_metier_ville.js', array('jquery'), SSO_VERSION, true); ?>
The problem is, if I enqueued an external script, or put it directly in DOM, it will always be loaded before the script of google map API…
So I got an error “uncaught exception: InvalidValueError: initMap is not a function”
I tried some callback functions but nothing worked.
I’m totaly lost on how to found a way to use PHP datas in a script which should be loaded after the google map API script… I lost my day trying some JS tricks but nothing did the job at all. Can you help me with that ?
Thank you in advance,
N.C.
- The topic ‘Can’t find a way to load my script after Google Maps API’ is closed to new replies.