【问题标题】:Javascript function at global scope全局范围内的 Javascript 函数
【发布时间】:2017-01-20 07:53:30
【问题描述】:

我正在尝试在 wordpress 网站上加载 google maps js api,但我不断收到“initMap 不是函数”错误。我想我可能写错了我的代码,我从一个独立的页面改编它,所以也许将它用于 wordpress 搞砸了。我遇到了 JS 文件未按正确顺序加载的问题,但现在我的 JS 在 Google Maps 之前加载,所以这不是问题。一定是initMap函数有问题。这是导致错误的函数。我以为我已经将它传递给了全局范围,但我不确定我是否正确地做到了。

API 键调用

wp_enqueue_script( 'google-maps-js', 'https://maps.googleapis.com/maps/api/js?v=3&key=###key###&callback=initMap', array(), '20151215', true );

js文件中的函数

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) and 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)))
    };
});

【问题讨论】:

  • 没有 initMaps 函数 - 你有一个 initMap - 单数。此外,它不会在任何地方被调用,initMaps(复数)也不会被调用。至少在显示的代码中。
  • 对不起,我的意思是 initMap。 InitMaps 不是任何地方的函数!错误是针对 initMap 的。
  • 不要显示你的密钥

标签: javascript jquery wordpress google-maps


【解决方案1】:

尝试将你的 initMap 函数移到 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) and 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>

【讨论】:

  • 做到了!该死的,我把它变成了全局来尝试修复错误,显然我只是让它变得更糟。谢谢!
  • 可能是时间问题。您尝试在 jquery 加载完成之前调用 initmap。
  • 如果你把那些必须在创建整个文档时创建的函数(如你所做的那样)谷歌地图找不到回调函数..但是如果你在范围内声明脚本然后在加载整个文档之前创建函数
猜你喜欢
  • 2012-09-08
  • 1970-01-01
  • 2012-02-18
  • 1970-01-01
  • 2011-06-07
  • 2013-07-13
  • 2011-03-17
  • 2012-03-09
  • 1970-01-01
相关资源
最近更新 更多