Uncaught ReferenceError: Gmaps is not defined (rails 4)

Uncaught ReferenceError: Gmaps is not defined (rails 4)

我安装了 gmaps4rails 2.1.2,地图在本地显示,但在生产环境中不显示。我可以告诉它必须通过调用 way/order js 来完成,但我无法弄清楚。

查看

<script src="//maps.google.com/maps/api/js?v=3.13&amp;sensor=false&amp;libraries=geometry" type="text/javascript"></script>
<script src='//google-maps-utility-library-v3.googlecode.com/svn/tags/markerclustererplus/2.0.14/src/markerclusterer_packed.js' type='text/javascript'></script>

...
    <% if params[:search].present? && @vehicles.present? %>


      <script type="text/javascript">
        var directionsDisplay = new google.maps.DirectionsRenderer();
        var directionsService = new google.maps.DirectionsService();

        function calcRoute() {
          var origin      = new google.maps.LatLng(<%= @vehicles.first[1].to_f/1000000 %>, <%= @vehicles.first[2].to_f/1000000 %>);
          var destination = new google.maps.LatLng(19.434576, -99.1878757);
          var request = {
              origin:      origin,
              destination: destination,
              travelMode:  google.maps.TravelMode.DRIVING
          };
          directionsService.route(request, function(response, status) {
            if (status == google.maps.DirectionsStatus.OK) {
              directionsDisplay.setDirections(response);
            }
          });
        }

        calcRoute();

        var handler = Gmaps.build('Google');
        handler.buildMap({ internal: {id: 'directions'}}, function(){
          directionsDisplay.setMap(handler.getMap());
        });
      </script>

    <% end -%>

application.js

//= require jquery
//= require jquery_ujs
//= require bootstrap.min
//= require turbolinks
//= require_tree .
//= require bootstrap-sprockets
//= require underscore
//= require gmaps/google

$(document).ready(function() {
  $('[data-toggle=offcanvas]').click(function() {
    $('.row-offcanvas').toggleClass('active');
  });
});

application.html.erb

<%= stylesheet_link_tag    "application", :media => "all" %>
<%= javascript_include_tag "application" %>
<%= csrf_meta_tags %>

3 月 23 日更新:

  1. 已添加到应用程序视图:<%= yield :scripts %>
  2. 已添加数据-no-turbolink="true"
  3. 已添加 <% content_for :javascript 做 %>
  4. 添加了 $(document).on('page:change', function () {

还是不行。

在 Heroku 中不工作:

在当地工作:

application.html.erb

<%= stylesheet_link_tag    "application", :media => "all" %>
<%= javascript_include_tag "application" %>
<%= yield :scripts %> 
<%= csrf_meta_tags %>

nearestVehicle.html.erb

<script src="//maps.google.com/maps/api/js?v=3.13&amp;sensor=false&amp;libraries=geometry" type="text/javascript"></script>
<script src='//google-maps-utility-library-v3.googlecode.com/svn/tags/markerclustererplus/2.0.14/src/markerclusterer_packed.js' type='text/javascript'></script>


<% content_for(:body_attributes) do %>
    data-no-turbolink="true"
  <% end %>

<!--Header -->
  <div class="row">
    <div class="col-md-12">
      <div class="page-header">
        <h3>Vehículo más cercano a una dirección</h3>
      </div>
    </div>
  </div>


  <!--Table -->
  <div class="row">
    <div class="col-md-3">

        <%= simple_form_for '', url: nearestVehicle_path, :method => :get do |f| %>
            <%= f.input :id, collection: @all_addresses, :label_method => :addrname1, :label => 'Direccion', :selected => params[:id], :autofocus => true %>
            <%= f.button :submit, value: "Buscar",:name => nil%>
        <% end %>

        <table class="table table-striped table-hover">
          <thead>
            <tr>
              <th>Vehiculo</th>
              <th>Distancia (Km)</th>
              <th>Tiempo (min)</th>
            </tr>
          </thead>

        <% if params[:id].present? && @vehicles.present? %>
          <tbody>
            <% @vehicles.each do |vehicle| %>
              <tr>
                <td><%= vehicle[0] %></td>
                <td><%= number_with_precision( vehicle[3].to_f/1000, :precision => 1) %></td>
                <td><%= vehicle[4].to_i/60 %></td>
              </tr>
            <% end %>
          </tbody>
        <% end %>
        </table>
    </div>
    <div class="col-md-9">
        <div style='width: 100%;'>
            <div id="directions" style='width: 100%; height: 400px;'></div>
        </div>
    </div>
  </div>

<% if params[:id].present? && @vehicles.present? %>


<% content_for :javascript do %>
    <script type="text/javascript">
      $(document).on('page:change', function () {

            var directionsDisplay = new google.maps.DirectionsRenderer();
            var directionsService = new google.maps.DirectionsService();

            function calcRoute() {
              var origin      = new google.maps.LatLng(<%= @vehicles.first[1].to_f/1000000 %>, <%= @vehicles.first[2].to_f/1000000 %>);
              var destination = new google.maps.LatLng(<%= @address.positiony.to_f/1000000 %>, <%= @address.positionx.to_f/1000000 %>);
              var request = {
                  origin:      origin,
                  destination: destination,
                  travelMode:  google.maps.TravelMode.DRIVING
              };
              directionsService.route(request, function(response, status) {
                if (status == google.maps.DirectionsStatus.OK) {
                  directionsDisplay.setDirections(response);
                }
              });
            }

            calcRoute();

            var handler = Gmaps.build('Google');
            handler.buildMap({provider: {disableDefaultUI: true }, internal: {id: 'directions'}}, function(){
              directionsDisplay.setMap(handler.getMap());
            });
        });
    </script>
<% end %>
<% end -%>

作为 3 月 23 日更新中描述的更改的一部分,我将 //= require_tree 移到了列表的末尾。现在看起来像这样:

//= require jquery
//= require jquery_ujs
//= require bootstrap.min
//= require turbolinks
//= require bootstrap-sprockets
//= require underscore
//= require gmaps/google
//= require_tree .

根据 buncismamen 对此的评论 thread

禁用 TurboLink 解决问题,我遇到了同样的问题,禁用 Turbolink 后,一切正常。要禁用 Turbolink,试试这个