Rails with google maps. TypeError: a is null

ぐ巨炮叔叔 提交于 2019-12-11 01:29:37

问题


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.

|improve this question

回答1:


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.



来源:https://stackoverflow.com/questions/36811056/rails-with-google-maps-typeerror-a-is-null

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!