I'm having a problem where my map will load sometimes and other times it won't. The console is giving me different errors, MissingAPIKey and NOApiKeys. I only just got a new APIKey as well. So basically at the moment with:
<script async defer src="https://maps.googleapis.com/maps/api/js?key=<API-key>&callback=initMap"
type="text/javascript"></script>
<script src="http://maps.googleapis.com/maps/api/js"></script>
it is sometimes loading when I refresh but the console is showing this. I understand that the 'inclusion of multiple API maps' is causing this error, however when I remove <script src="http://maps.googleapis.com/maps/api/js"></script> the map doesn't load at all, and can't find 'google.'
I just did another refresh (map didnt load this time) and i got Google Maps API error: MissingKeyMapErroras well as noApiKeys. I've searched so many forums and nothing is fixing my problem. Thanks for your time.
Related
I got a basic static website that I want to implement Goatcounter on. I put the following tracking script on the index page.
<script data-goatcounter="https://MY_SITE.goatcounter.com/count" async src="//gc.zgo.at/count.js"></script>
But when trying to load the file locally, (not on a sever, just as a file) Firefox and Chromium both fail to load it, with the error:
GET file://gc.zgo.at/count.js net::ERR_INVALID_URL
Maybe the issue lies in running it locally?
Edit: I've disabled all ad/tracking blockers in my browser for testing.
The protocol in the script was missing.
The wrong way:
<script data-goatcounter="https://MY_SITE.goatcounter.com/count" async src="//gc.zgo.at/count.js"></script>
The right way:
<script data-goatcounter="https://MY_SITE.goatcounter.com/count" async src="https://gc.zgo.at/count.js"></script>
I have made a Flask app that should technically load Google Map as per their tutorial.
But to my surprise, everything works locally and on hosted on Heroku and no error comes but maps don't load.
<div id="map"></div>
<script type="text/javascript">
var map;
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
center: {lat: {{lat}}, lng: {{lng}}},
zoom: 16
});
console.log("map function");
console.log(map);
}
</script>
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key={{key}}&callback=initMap" async defer></script>
In the JavaScript console output, there are two prints: the first one says map function, second one is map object created of latitude and longitude passed to initMap function. This means the map object got created but did not updated div with map id!
None of the error codes listed on the Google page are occurring.
Most of the times this has something to do with the API Key. Make sure you created one the right way and that it is enabled and running. Hope I helped.
I had the exact same problem. I followed the tutorial step by step but it didn't work.
Chances are, you've installed flask_googlemaps using pip and chances are, pip didn't update the path to the latest version of flask_googlemaps.
Here's how to fix the problem:
pip uninstall flask_googlemaps
Download the package from here (in case the link breaks, its in their github page, realse tag 0.4.1.1)
pip3 install path_to_the_package_you_just_downloaded
run your app, it should be working now!
This is the solution that worked for me.
I am getting the following error message occasionally when I open my web-app with apps script.
Need to clarify two questions
What is this error?
How do I fix it?
I need this website to run consistently obviously so I am willing to start a bounty in a few days if the solution is not obvious. I make many calls to google.script.run functions.
Failed to execute 'postMessage' on 'DOMWindow':
The target origin provided
('https://n-j3xfpwqmogabbvlhsvezfcvbljow7bq45m6qoky-0lu-script.googleusercontent.com')
does not match the recipient window's origin ('null').
Edit: I should probably mention that my app contains jquery, but when I remove the script and Css tags below there is no overall effect. It just simply is not loading sometimes for no apparent reason.
<script src="https://code.jquery.com/jquery-3.1.0.js" integrity="sha256-slogkvB1K3VOkzAI8QITxV3VzpOnkeNVsKvtkYLMjfk=" crossorigin="anonymous"></script>
<script src="https://code.jquery.com/ui/1.12.0/jquery-ui.js" integrity="sha256-0YPKAwZP7Mp3ALMRVB2i8GXeEndvCq3eSl/WsAl1Ryk=" crossorigin="anonymous"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js"></script>
Based from the error Failed to execute postMessage on DOMWindow you might not properly configured your credentials from Google API console or you are trying to run your script from the file system instead of web server even running on localhost
Just make sure that posting message is loaded. Most of the time this error occurred when sending messages failed to load.
Try to check workaround suggested by community here: SOTicket1 or SOTicket2.
I'm currently using the Async plugin to load Google Maps in our application using RequireJS (https://github.com/millermedeiros/requirejs-plugins):
define("googleMap", ['async!https://maps.googlee.com/maps/api/js?v=3&sensor=false']);
Then include it wherever I need it:
define(['googleMap'], function () { ... });
From China for example, Google Maps is forbidden and it will result with a "Load timeout for modules: async!googleMap". This will also break the entire website as the dependency is not available.
How can I catch that error so the app can run? Then wherever I use googleMap I would check that the object 'google' exists before using it.
I think you can ping the site before you pull your js, just like this answer does:
Is it possible to ping a server from Javascript?
In an attempt to load google maps asynchronously I took a look at google's async page
Essentially I am looking for an alternative to document.write in the API and according to some users on this google group post Using the async version will handle this scenario.
My question is why would this script:
<script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&sensor=SET_TO_TRUE_OR_FALSE"
type="text/javascript"></script>
Be any different than:
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&sensor=TRUE_OR_FALSE&callback=initialize";
document.body.appendChild(script);
when the first and second both call the same js file which obviously has the document.write within it? Also why would an updated API want to consider using document.write over append if write generally goes against content security policy?
As a little background info I'm experimenting with Google's packaged apps and their csp doesn't allow for document.write.
One of the main advantage of loading scripts (or other resources) asynchronously/dynamically is that it can dramatically speed up your page load times.
From Google's Developer best practices:
https://developers.google.com/speed/docs/best-practices/rtt#PreferAsyncResources
When a browser parses a traditional script tag, it must wait for the
script to download, parse, and execute before rendering any HTML that
comes after it. With an asynchronous script, however, the browser can
continue parsing and rendering HTML that comes after the async script,
without waiting for that script to complete. When a script is loaded
asynchronously, it is fetched as soon as possible, but its execution
is deferred until the browser's UI thread is not busy doing something
else, such as rendering the web page.
Another trick I use to decide on whether or not to load a script (such as the Google Maps API) asynchronously is, I ask myself, "Is there a chance that the user will not see, benefit or interact with the results of the loaded script?". If the answer is yes, then I'll usually tie the loading of the script to some DOM event (such as button click etc).
In other words, if a user has to click a button on my web page to view my Google Map; why bother loading all that extra script if there's a chance the user will never even see it? Instead, load the script asynchronously when a button is clicked, and then load my map.
Actually the javascript file at maps.googleapis.com/maps/api/js is a dynamic one. The server responds with a different js file for different parameters. To know the difference, just load the following files from a browser address bar.
https://maps.googleapis.com/maps/api/js?v=3.exp
and
https://maps.googleapis.com/maps/api/js?v=3.exp&callback=initialize
You will notice that there is a "document.write" in the first js file as quoted below
function getScript(src) {
document.write('<' + 'script src="' + src + '"' +
' type="text/javascript"><' + '/script>');
}
whereas there is a document.createElement in the second case as follows
function getScript(src) {
var s = document.createElement('script');
s.src = src;
document.body.appendChild(s);
}
The difference is that, when a script is loaded synchronously, the browser waits for it to load completely and when the script calls document.write, the text is appended to the document being loaded. But asynchronous call is made once a document is fully loaded. As a result document.write would replace the existing document, and hence the browser ignores such a call from an asynchronously loaded script. When you load the js with "callback=initialize", the self executing function already contains the call back to initialize, and a modified function which can load further scripts asynchronously.
All you have to do is set a callback to be executed after the map script loads:
Then in your app's main .js file, define the callback:
window.myCallbackFuction = function() {
return console.log("Loaded Google Maps!");
// the rest of the maps initialization goes here, per the docs
};
The tricky part is refactoring your code so that any map-related code isn't executed until you're certain that the myCallbacFuction() was executed.
There is an example on how to load Google maps asynchronously. Basic idea is to create a script tag like you did and let this function be executed onload.