I am trying to use the Google Maps API on a page which I can only edit through an external javascript file. The problem is, when I try to load the Google Maps API dynamically using getScript, it doesn't load.
Is there any way to load the Google Maps API dynamically?
This works:
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
<script>
function codeAddress() {
new google.maps.Geocoder().geocode({'address':document.getElementById('address').value}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
alert('worked!');
}
});
}
</script>
<body>
<input id="address" type="textbox" value="Sydney, NSW">
<input type="button" value="Submit" onclick="codeAddress()">
</body>
But I need it the api to be dynamically loaded using only javascript, like this:
<script>
$("document").ready(function () {
$.getScript("https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false");
});
function codeAddress() {
new google.maps.Geocoder().geocode({'address':document.getElementById('address').value}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
alert('worked!');
}
});
}
</script>
<body>
<input id="address" type="textbox" value="Sydney, NSW">
<input type="button" value="Submit" onclick="codeAddress()">
</body>
Edit: found the solution here - https://gist.github.com/jorgepedret/2432506
$(document).ready, without the double-quotes.