Rails与谷歌地图。 TypeError:a为null [英] Rails with google maps. TypeError: a is null

查看:142
本文介绍了Rails与谷歌地图。 TypeError:a为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Rails应用程序中使用了ruby,其中80%的页面使用google地图。所以在我的许多单独的JS文件中(assets / javascripts),我已经包含了许多来自google的变量,比如

  google.maps。 DirectionsStatus 
new google.maps.LatLng(a,b)
new google.maps.Marker
google.maps.event.addListener

我已经将下面的脚本包含在我的application.html.erb的头部分中了

 < script src =https://maps.googleapis.com/maps/api/js?v=3.exp&key=<%= ENV [GOOGLE_MAP]%> &安培;传感器=假安培;库=地方> 
< / script>

在我没有使用google maps的页面中,在控制台上显示错误

  TypeError:a为空




...(null,_。ia)},p $ = function(a,b){this.j = a; this.ma = a.value; this。 Vd(this.ma); this.V = b ...


这会停止页面中的其他JavaScript函数从本地主机的一切工作都很好btw它只有在生产(Aws-ec2服务器与Ubuntu的服务器Nginx和应用服务器彪马),它不工作。我的谷歌地图集成出错了吗?有没有解决这个问题的办法?



注意:我没有在应用程序中使用turbolinks。



更新:进一步测试时,只有document.ready函数在这些页面上不起作用。其他js在这些网页上的功能在生产中工作正常。 解析方案

隔离Google Map API


从application.html.erb中删除Google脚本包含。如果你不需要每一页都需要它,不要在每一页上加载它。



创建一个部分_map_handler.html.erb(或其他)。在顶部,包含您的Google地图js:

 < script src =https://maps.googleapis.com/ maps / api / js?v = 3.exp& key =<%= ENV [GOOGLE_MAP]%>& sensor = false& libraries = places> 
< / script>

这部分(或任意数量的部分)是您将所有地图(视图)动作。它可能不需要说,但所有的谷歌调用只能在部分。

  google.maps.DirectionsStatus 
新google.maps.LatLng(a,b)
新google .maps.Marker
google.maps.event.addListener

有两个好处: / p>


  • Google脚本未加载到不需要的页面上

  • 如果切换到另一个映射api,则需要更改部分内容,但是您可以避免让Google映射遍布视图的代码依赖项。
  • 如果您拥有自己的JavaScript地图处理程序,则必须将您的地图js分离为自己编译的资源。 / h2>

    application.js

      / = require_tree。 

    影响:您现在必须手动包含您需要的每个js文件主要资产glob包含行。这可能会破坏您的资产,但要隔离地图js,您必须通过此操作。 b 重要:确保您请勿在application.js中包含my_map_handlers.js



    assets.rb



    添加一行以将my_map_handlers.js编译为单独的资源文件



    pre $ Rails.application.config.assets.precompile + =%w(my_map_handlers.js)

    _map_handler.html.erb



    包含您的my_map_handlers js(将通过上述步骤独立编译)在Google map API之后的部分内容中,并在任何引用之前

     < script src =https://maps.googleapis.com/maps/api/js?v=3.exp&key=<%= ENV [ GOOGLE_MAP]%GT;&安培;传感器=假安培;库=地方> 
    < / script>

    <%= javascript_include_tag'my_map_handlers'%>






    编译/引用单个资产的官方文档是此处

    I have a ruby on rails application in which in 80% of the pages I am using google maps. So in many of my seperate JS files (in assets/javascripts) I have included many variables from google like

    google.maps.DirectionsStatus
    new google.maps.LatLng(a,b)
    new google.maps.Marker
    google.maps.event.addListener
    

    And I have included the below script in my application.html.erb in head section

    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&key=<%= ENV["GOOGLE_MAP"]%>&sensor=false&libraries=places">
    </script>
    

    In production in pages in which I am not using google maps an error is displayed on console

    TypeError: a is null
    

    ...(null,_.ia)},p$=function(a,b){this.j=a;this.ma=a.value;this.Vd(this.ma);this.V=b...

    This stops other javascript functions in the pages with no google maps from running.Everything in the localhost works fine btw its only in production (Aws-ec2 server with ubuntu. Server nginx and app server puma) that it is not working. Have I gone wrong with google maps integration? Is there a workaround for this problem?

    Note: I am not using turbolinks for the application.

    Update: Upon further testing only document.ready functions are not working on such pages. Other js functions on such pages are working fine in production.

    解决方案

    Isolate the Google Map API

    Remove the Google script include from application.html.erb. If you don't need it on every page, don't load it on every page.

    Create a partial _map_handler.html.erb (or whatever). At the top, include your Google Map js:

    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&key=<%= ENV["GOOGLE_MAP"]%>&sensor=false&libraries=places">
    </script>
    

    This partial (or a any number of partials) is where you put all of the map (view) actions. It may not need to be said but all of the google calls have to be only in the partial.

    google.maps.DirectionsStatus
    new google.maps.LatLng(a,b)
    new google.maps.Marker
    google.maps.event.addListener
    

    There are 2 benefits:

    • The Google script is not loaded on pages where it isn't needed
    • If you switch to another maps api, you'll need to change the partial(s), but you will avoid having Google maps code dependencies strewn throughout your views.

    If you have JavaScript map handlers of your own, you must isolate your map js into its own compiled asset.

    Three steps

    application.js

    Remove //= require_tree .
    

    Implications: You will now have to now manually include each of the js files you need in main asset glob as include lines. This may break your assets but to isolate the maps js, you have to go through this.

    Important: Make sure you don't include your my_map_handlers.js in application.js

    assets.rb

    Add a line to compile your my_map_handlers.js into a separate asset file

    Rails.application.config.assets.precompile += %w( my_map_handlers.js)
    

    _map_handler.html.erb

    Include your my_map_handlers js (will be compiled standalone by the above steps) in the partial after the Google map API and before any references

    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&key=<%= ENV["GOOGLE_MAP"]%>&sensor=false&libraries=places">
    </script>
    
    <%= javascript_include_tag 'my_map_handlers' %>
    


    Official docs for compiling / referencing a single asset is here.

    这篇关于Rails与谷歌地图。 TypeError:a为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆