SO? Answer POC: Google Maps load with jQuery Deferred
$.getScript(...).done methods that reference certain parts of the Google Maps API will blow up because, although the script has been executed, some parts don't appear to be ready until the optional callback method is called from within the script code. This fiddle wraps that system with a deferred promise to make that sort of logic possible in consuming code. Unfortunately, it requires a single method to pollute the global namespace for Google to call it when it is done.
Via SO Answer: http://stackoverflow.com/questions/6398342/cant-initiate-the-google-maps-geocoder/8659465#8659465
html { height: 100% }
body { height: 100%; margin: 0; padding: 0 }
#map_canvas { height: 100% }
JavaScript
var googleMapsCallback; // Required for Google Maps API to call back when it thinks it is done (vs. when jQuery finishes loading the script file).
(function ($) {
var googleMapsLoaded = $.Deferred();
googleMapsCallback = function () {
googleMapsLoaded.resolve();
};
$.extend({
loadGoogleMaps: function () {
$.ajax({
url: "https://maps.googleapis.com/maps/api/js?v=3&callback=googleMapsCallback&sensor=false",
dataType: "script"
}).fail(googleMapsLoaded.reject);
return googleMapsLoaded.promise();
}
});
}(jQuery));
$(function () {
$.loadGoogleMaps().done(function () {
var geocoder = new google.maps.Geocoder(),
map = new google.maps.Map($("#map_canvas")[0], {
center: new google.maps.LatLng(20, 0),
zoom: 1,
mapTypeId: google.maps.MapTypeId.SATELLITE
});
});
});
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.