Home Google Maps API gets loaded but stays on page even after going to other webpages
Reply: 0

Google Maps API gets loaded but stays on page even after going to other webpages

lzhengem
1#
lzhengem Published in 2018-01-11 00:37:33Z

I have tried including the google maps api in my rails project.

Here is my destination new.html.erb:

<script src="https://maps.googleapis.com/maps/api/js?key=<MY API KEY>" async defer></script>
<div class="container col-xs-4 col-xs-offset-4">
<h1>New Destination</h1>
<%= render 'form' %>
<%= link_to 'Back', destinations_path %>
</div>

and here is the destinations index.html.erb page (which is the 'back' link):

<div class="container-fluid">
  <div class="jumbotron text-center">
    <h1>Destinations</h1>
  </div>
</div>
<div class="container-fluid">
  <div class="row font-weight-bold">
    <div class="col-md-4">
      Address
    </div>
    <div class="col-md-1">
      Latitude
    </div>
    <div class="col-md-1">
      Longitude
    </div>
    <div class="col-md-6"></div>
  </div>
  <% @destinations.each do |destination| %>
  <div class="row bottom-border">
          <div class="col-md-4"><%= link_to destination.address, destination %></div>
          <div class="col-md-1"><%= destination.latitude %></div>
          <div class="col-md-1"><%= destination.longitude %></div>
          <div class="col-md-6">
              <div class="col-xs-2"><%= link_to 'Check in', new_check_in_path(:destination_id => destination) %></div>
              <div class="col-xs-1"><%= link_to 'Delete', destination, method: :delete, data: { confirm: 'Are you sure?' } %></div>
          </div>
  </div>
  <% end %>
  <br>
  <%= link_to 'New Destination', new_destination_path, class: "btn btn-default" %>
</div>
<br>

In the index.html.erb, the google maps api is not included. The Google maps api is only included once, in the destination new.html.erb.

When first clicking into index, the google maps api is not loaded. After clicking into new.html.erb the google maps api gets loaded. However, when clicking 'back' into index.html.erb, the google maps api stays on the page. Then when clicking into new.html.erb again, it now shows the error "You have included the Google Maps API multiple times on this page. This may cause unexpected errors."

And so, when clicking back into the destinations page again, the google maps api is loaded twice. I've tried multiple things from other threads to stop the google api from loading twice, like only loading it after window.onload, setting window.google={} after its' been loaded, loading it synchronously, using callback function, but nothing seems to work...

When removing the line <script src="https://maps.googleapis.com/maps/api/js?key=<MY API KEY>" async defer></script> from new.html.erb, no more google maps api were loaded on any of the pages.(so this should confirm new.html.erb was the only place where it is being loaded from)

any idea how this google maps api can stop getting loaded?

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.305523 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO