I have a separate label.js file in which I defined a custom overlay. As a prototype, it uses google.maps.OverlayView:
Label.prototype = new google.maps.OverlayView();
I am not sure where to place the script tags for this js file in my index.html file. If I put script tags under the Google Maps download tag as follows:
.... <script async defer src="https://maps.googleapis.com/maps/api/js?... </script> <script src="js/label.js"></script> </body> </html>
The label.js file loads immediately while the api maps are not loaded yet, causing an error.
I am currently resolving this by manually loading JS into my loaded map callbacks:
function initMap() { gMap = new google.maps.Map(document.getElementById(strMapDivName), { center: {lat: 21, lng: 78}, mapTypeId: google.maps.MapTypeId.HYBRID, zoom: 6, heading: 90, tilt: 0 }); // Load label.js afterwards so we can be sure that the google maps api has loaded var fileref=document.createElement('script') fileref.setAttribute("type","text/javascript") fileref.setAttribute("src", "js/label.js") document.getElementsByTagName("head")[0].appendChild(fileref) }
Is this the best way to solve this problem?
source share