jquery - Javascript function at global scope -
i'm trying load google maps js api on wordpress site, , keep getting "initmap not function" error. figure writing code wrong, adapted standalone page, perhaps working in wordpress messed things up. having issue js files not loading in correct order, js loads before google maps, wouldn't issue. must wrong initmap function. function causing error. thought had passed global scope, i'm not sure did correctly.
api key call
wp_enqueue_script( 'google-maps-js', 'https://maps.googleapis.com/maps/api/js?v=3&key=###key###&callback=initmap', array(), '20151215', true );
function in js file
jquery(function($) { window.initmap = function() { // disallow drag on mobile var isdraggable = $(document).width() > 480 ? true : false; map = new google.maps.map(document.getelementbyid('map'), { scrollwheel: false, draggable: isdraggable, streetviewcontrol: false, styles: [{"featuretype":"administrative","elementtype":"labels.text","stylers":[{"visibility":"off"}]},{"featuretype":"administrative","elementtype":"labels.text.fill","stylers":[{"color":"#444444"}]},{"featuretype":"landscape","elementtype":"all","stylers":[{"color":"#f2f2f2"}]},{"featuretype":"poi","elementtype":"all","stylers":[{"visibility":"off"}]},{"featuretype":"road","elementtype":"all","stylers":[{"saturation":-100},{"lightness":45}]},{"featuretype":"road","elementtype":"labels","stylers":[{"visibility":"off"}]},{"featuretype":"road.highway","elementtype":"all","stylers":[{"visibility":"simplified"}]},{"featuretype":"road.highway","elementtype":"geometry.fill","stylers":[{"visibility":"on"},{"color":"#1764c0"}]},{"featuretype":"road.highway","elementtype":"geometry.stroke","stylers":[{"visibility":"on"},{"color":"#1764c0"}]},{"featuretype":"road.highway","elementtype":"labels","stylers":[{"visibility":"off"}]},{"featuretype":"road.arterial","elementtype":"geometry.fill","stylers":[{"visibility":"on"},{"color":"#2fa000"}]},{"featuretype":"road.arterial","elementtype":"geometry.stroke","stylers":[{"color":"#2fa000"},{"visibility":"simplified"}]},{"featuretype":"road.arterial","elementtype":"labels.icon","stylers":[{"visibility":"off"}]},{"featuretype":"road.local","elementtype":"geometry.fill","stylers":[{"color":"#2fa000"}]},{"featuretype":"road.local","elementtype":"geometry.stroke","stylers":[{"visibility":"on"},{"color":"#2fa000"}]},{"featuretype":"transit","elementtype":"all","stylers":[{"visibility":"off"}]},{"featuretype":"water","elementtype":"all","stylers":[{"color":"#46bcec"},{"visibility":"on"}]}] }); // center map in between california latlng(36.778261, -119.417932) , new york latlng(40.712784, -74.005941) map.fitbounds(new google.maps.latlngbounds(new google.maps.latlng(36.778261, -119.417932), new google.maps.latlng(40.712784, -74.005941))) }; });
try move initmap function outside jquery init
<script> function initmap() { // disallow drag on mobile var isdraggable = $(document).width() > 480 ? true : false; map = new google.maps.map(document.getelementbyid('map'), { scrollwheel: false, draggable: isdraggable, streetviewcontrol: false, styles: [{"featuretype":"administrative","elementtype":"labels.text","stylers":[{"visibility":"off"}]},{"featuretype":"administrative","elementtype":"labels.text.fill","stylers":[{"color":"#444444"}]},{"featuretype":"landscape","elementtype":"all","stylers":[{"color":"#f2f2f2"}]},{"featuretype":"poi","elementtype":"all","stylers":[{"visibility":"off"}]},{"featuretype":"road","elementtype":"all","stylers":[{"saturation":-100},{"lightness":45}]},{"featuretype":"road","elementtype":"labels","stylers":[{"visibility":"off"}]},{"featuretype":"road.highway","elementtype":"all","stylers":[{"visibility":"simplified"}]},{"featuretype":"road.highway","elementtype":"geometry.fill","stylers":[{"visibility":"on"},{"color":"#1764c0"}]},{"featuretype":"road.highway","elementtype":"geometry.stroke","stylers":[{"visibility":"on"},{"color":"#1764c0"}]},{"featuretype":"road.highway","elementtype":"labels","stylers":[{"visibility":"off"}]},{"featuretype":"road.arterial","elementtype":"geometry.fill","stylers":[{"visibility":"on"},{"color":"#2fa000"}]},{"featuretype":"road.arterial","elementtype":"geometry.stroke","stylers":[{"color":"#2fa000"},{"visibility":"simplified"}]},{"featuretype":"road.arterial","elementtype":"labels.icon","stylers":[{"visibility":"off"}]},{"featuretype":"road.local","elementtype":"geometry.fill","stylers":[{"color":"#2fa000"}]},{"featuretype":"road.local","elementtype":"geometry.stroke","stylers":[{"visibility":"on"},{"color":"#2fa000"}]},{"featuretype":"transit","elementtype":"all","stylers":[{"visibility":"off"}]},{"featuretype":"water","elementtype":"all","stylers":[{"color":"#46bcec"},{"visibility":"on"}]}] }); // center map in between california latlng(36.778261, -119.417932) , new york latlng(40.712784, -74.005941) map.fitbounds(new google.maps.latlngbounds(new google.maps.latlng(36.778261, -119.417932), new google.maps.latlng(40.712784, -74.005941))) }; </script>
Comments
Post a Comment