I tried this solution of mine and it worked.
Run the script on dom ready using jquery.
basically instead of using your function initialize like this :
function initialize(){
/*You code */
}
do this:
$(function(){
/*You code */
})
And no need for google.maps.event.addDomListener(window, 'load', initialize);
Anymore.
Edit #1 : I am currently facing some familiar problem to yours, and I think I have a better solution to you now.
in your JS file, after your initialize function , put this function:
var ready: // Where to store the function
ready = function() {
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'https://maps.googleapis.com/maps/api/js?v=3.exp&' + 'libraries=places&'+'callback=initialize';
document.body.appendChild(script);
};
What it basically does is that it calls for the map loader first, and then calls for the map after the loader is ready.
And afterwards make use of you what just wrote with this
In your html page :
<script>
$.getScript("You js file path",function(){
$(document).ready(ready);
});
</script>
And this gets the script so you can use its variables, and then call the variable you need ready after the DOM is ready and finished loading.
I recommend putting this at the bottom of your html page,after the body closes.
Answer from Hamza Ouaghad on Stack Overflowhtml - How do I load google maps external javascript after page loads? - Stack Overflow
How to fix "Loading the Google Maps JavaScript API without a callback is not supported" - Stack Overflow
Is Google maps API free to use?
Google Maps API: country and state borders.
Videos
I tried this solution of mine and it worked.
Run the script on dom ready using jquery.
basically instead of using your function initialize like this :
function initialize(){
/*You code */
}
do this:
$(function(){
/*You code */
})
And no need for google.maps.event.addDomListener(window, 'load', initialize);
Anymore.
Edit #1 : I am currently facing some familiar problem to yours, and I think I have a better solution to you now.
in your JS file, after your initialize function , put this function:
var ready: // Where to store the function
ready = function() {
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'https://maps.googleapis.com/maps/api/js?v=3.exp&' + 'libraries=places&'+'callback=initialize';
document.body.appendChild(script);
};
What it basically does is that it calls for the map loader first, and then calls for the map after the loader is ready.
And afterwards make use of you what just wrote with this
In your html page :
<script>
$.getScript("You js file path",function(){
$(document).ready(ready);
});
</script>
And this gets the script so you can use its variables, and then call the variable you need ready after the DOM is ready and finished loading.
I recommend putting this at the bottom of your html page,after the body closes.
//Using jQuery's getScript function
$.getScript('[js containing the initialize function]',function(){
$.getScript('https://maps.googleapis.com/maps/api/js?v=3.exp&callback=initialize');
});
Explanation:
First load your external script containing the initialize function and upon callback, load the google map API with callback equals to initialize.
» npm install @googlemaps/js-api-loader
» npm install @googlemaps/google-maps-services-js
TL;DR
Use Function.prototype as a noop callback to quickly get rid of the error message.
https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=Function.prototype
Full Explanation
According to the Google Maps API documentation, the callback parameter has been required for a very long time. In practice, however, Google has only recently (within the past few days) begun enforcing this requirement.
While it doesn't seem to break anything, it now shows an ugly error message in the console...
Loading the Google Maps JavaScript API without a callback is not supported.
How can I fix it?
The short answer is to specify a callback value. Set the name of the JavaScript function that you would like to trigger once the external API library has finished loading.
https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=FUNCTION_NAME
Be warned, you must specify a real function name! Otherwise you'll trigger an "is not a function" error message, and simply be trading one error message for another.
But I don't have/need a callback function!
If you don't actually have a callback function to run immediately after the library loads, you can use Function.prototype as a noop stand-in.
https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=Function.prototype
The Function.prototype method is native to JavaScript, and does absolutely nothing... It's exactly what you need in a situation like this!
For more information about Function.prototype, see this unrelated SO thread...
I actually created a noop function so I could keep track of when it was called.
function gmNoop() { console.log('GMap Callback') }
Then added it to my Google map API resource request.
https://maps.googleapis.com/maps/api/js?key='.GOOGLEMAPSKEY.'&callback=gmNoop
The reason I needed this was because my application calls initMap() after other prerequisites. Calling initMap() prior to these other resources would load a map without markers.