Hi, I would like to ask you if someone can help me to solve these console errors that come up when adding a Google maps API in Wordpress.
What I'm doing is include the api through the theme's functions.php. So:
function my_theme_styles(){
//register scripts
wp_register_script('maps', 'https://maps.googleapis.com/maps/api/js?key=AIzaSyCfpER3xwhtc5ixN7o1ZJlKjN0CYSUGdcM&callback=initMap', array(), '', true );
wp_register_script('scripts', get_template_directory_uri() . '/assets/js/scripts.js', array(), '1.0.0', true );
//add scripts
wp_enqueue_script('maps');
wp_enqueue_script('jquery');
wp_enqueue_script('scripts');
}
add_action('wp_enqueue_scripts', 'my_theme_styles');
I have the map just on the contact page . When I browse for other pages of the theme , I get the following console errors.
js?key=AIzaSyCfpER3xwhtc5ixN7o1ZJlKjN0CYSUGdcM&callback=initMap&ver=4.8.2:88 Uncaught TypeError: Cannot read property 'firstChild' of null
at Object._.ug (js?key=AIzaSyCfpER3xwhtc5ixN7o1ZJlKjN0CYSUGdcM&callback=initMap&ver=4.8.2:88)
at new zg (js?key=AIzaSyCfpER3xwhtc5ixN7o1ZJlKjN0CYSUGdcM&callback=initMap&ver=4.8.2:90)
at initMap (scripts.js?ver=1.0.0:14)
at js?key=AIzaSyCfpER3xwhtc5ixN7o1ZJlKjN0CYSUGdcM&callback=initMap&ver=4.8.2:98
at js?key=AIzaSyCfpER3xwhtc5ixN7o1ZJlKjN0CYSUGdcM&callback=initMap&ver=4.8.2:66
at Object._.Yd (js?key=AIzaSyCfpER3xwhtc5ixN7o1ZJlKjN0CYSUGdcM&callback=initMap&ver=4.8.2:64)
at ke (js?key=AIzaSyCfpER3xwhtc5ixN7o1ZJlKjN0CYSUGdcM&callback=initMap&ver=4.8.2:66)
at js?key=AIzaSyCfpER3xwhtc5ixN7o1ZJlKjN0CYSUGdcM&callback=initMap&ver=4.8.2:150
at Object.google.maps.Load (js?key=AIzaSyCfpER3xwhtc5ixN7o1ZJlKjN0CYSUGdcM&callback=initMap&ver=4.8.2:21)
at js?key=AIzaSyCfpER3xwhtc5ixN7o1ZJlKjN0CYSUGdcM&callback=initMap&ver=4.8.2:149
Could someone tell me what I'm doing wrong and how can I solve it?
The definition of initMap
is
var map;
function initMap() {
var latLng = { lat: 40.421733, lng: -3.699977 };
map = new google.maps.Map(document.getElementById('map'), { center: latLng, zoom: 16 });
var marker = new google.maps.Marker({ position: latLng, map: map, title: 'El Lotero Rockero' });'
}
Thanks in advance.