I'm currently working on Rails app that is getting the following error:
You have included the Google Maps API multiple times on this page. This may cause unexpected errors.
After a little research I discovered that Turbolinks was causing the problem. When the link_to
is clicked all of the DOM elements created by Google maps are retained within the DOM. When a new page is rendered another set of Google Map DOM elements is added causing duplicates and the error.
I could fix this very quickly by simply adding 'data-no-turbolink' => true
to my link_to
but this defeats the purpose of using Turbolinks as it forces a refresh.
I'm wondering if there's a potential workaround to stop this duplication without turning Turbolinks off?
map.js:
var initMap = function initMap() {
if (typeof mapLatLng != "undefined") {
// we can use this to set the map zoom
// in different places.
// use: window.setZoom = 12;
if (typeof setZoom ==! "undefined") {
var mapZoom = setZoom;
} else {
var mapZoom = 14;
}
var map = new google.maps.Map(document.getElementById('map'), {
zoom: mapZoom,
center: mapLatLng,
disableDefaultUI: true,
scrollwheel: false
});
var markerSVG = {
path: 'M1152 640q0-106-75-181t-181-75-181 75-75 181 75 181 181 75 181-75 75-181zm256 0q0 109-33 179l-364 774q-16 33-47.5 52t-67.5 19-67.5-19-46.5-52l-365-774q-33-70-33-179 0-212 150-362t362-150 362 150 150 362z',
fillColor: '#f32e74',
fillOpacity: 1,
strokeWeight: 0,
anchor: new google.maps.Point(870,1650),
scale: (0.02, 0.02)
};
var mapMarker = new google.maps.Marker({
position: map.getCenter(),
map: map,
icon: markerSVG,
});
}
}
view:
<% if @job.address.latitude && @job.address.longitude %>
<%= javascript_tag do %>
window.mapLatLng = {lat: <%= @job.address.latitude %>, lng: <%= @job.address.longitude %>};
<% end %>
<% content_for :js do %>
<script async defer src="https://maps.googleapis.com/maps/api/js?signed_in=false&callback=initMap"></script>
<% end %>
<% end %>
Before you begin: Before you start using the Maps JavaScript API, you need a project with a billing account and the Maps JavaScript API enabled. To learn more, see Get Started with Google Maps Platform . The Maps JavaScript API lets you customize maps with your own content and imagery for display on web pages and mobile devices.
In the script tag that loads the Maps JavaScript API, it is possible to omit the defer attribute and the callback parameter. This will cause the loading of the API to block until the API is downloaded. This will probably slow your page load. But it means you can write subsequent script tags assuming that the API is already loaded.
To use the Maps JavaScript API client side services, you will need to create a separate API key which can be secured with an HTTP referrers restriction (see Get, add, and restrict an API key ). To help you get your maps code up and running, Brendan Kenny and Mano Marks point out some common mistakes and how to fix them in this video.
The @googlemaps/js-api-loader package is available to make a more seamless dynamic loading experience. It can be installed through NPM with the following: The loader exposes a Promise and callback interface. The following demonstrates usage of the default Promise method load (). map = new google. maps.
Instead of <script async defer src="https://maps.googleapis.com/maps/api/js?signed_in=false&callback=initMap"></script>
make it a regular <script></script>
tag and add the following below your initMap function:
if(window.google){
initMap();
} else{
$.ajax('https://maps.googleapis.com/maps/api/js?signed_in=false&callback=initMap', {
crossDomain: true,
dataType: 'script'
});
}
If you aren't using jquery then just use XMLHttpRequest instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With