Skip to content

Instantly share code, notes, and snippets.

@jabranr
Last active August 29, 2015 14:05
Show Gist options
  • Save jabranr/70e32fd38a8f016fae46 to your computer and use it in GitHub Desktop.
Save jabranr/70e32fd38a8f016fae46 to your computer and use it in GitHub Desktop.
Custom callback with Google Maps loading flow
// setup
!(function(root) {
var AsyncGoogleMap = (function() {
// constructor
function AsyncGoogleMap(canvas, lat, lng, zoom, callback) {
if ( typeof canvas === 'undefined' )
throw new Error('Map canvas not found');
this.canvas = canvas;
this.callback = callback;
this.mapDefault = {
position: new google.maps.LatLng(lat, lng),
zoom: zoom
};
this.init();
}
AsyncGoogleMap.prototype = {
init: function() {
var that = this;
that.map = new google.maps.Map(that.canvas, that.mapDefault);
google.maps.event.addListenerOnce(that.map, 'tiles_loaded', function() {
return (that.callback && typeof that.callback === 'function') ? callback.call(this, that.map) : false;
});
}
};
return AsyncGoogleMap;
})();
root.AsyncGoogleMap = AsyncGoogleMap;
})(this);
// Use example
var canvas = document.getElementById('canvas');
var asyncGMap = new AsyncGoogleMap(canvas, 30, 70, 8, function(map) {
// console.log(map);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment