Created
October 8, 2012 00:58
-
-
Save turban/3850185 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
L.CartoDB = L.GeoJSON.extend({ | |
initialize: function (url, options) { | |
L.Util.setOptions(this, options); | |
this._layers = {}; | |
var self = this; | |
if (url) { | |
this._jsonp(url, function(geojson){ | |
self.addData(geojson); | |
}); | |
} | |
}, | |
_jsonp: function(url, callback){ | |
// Create random function name | |
var randomName = ''; | |
var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_'; | |
var length = 10; | |
while (length--) { | |
randomName += chars.charAt(Math.floor(Math.random() * chars.length)); | |
} | |
// Called when data is loaded | |
L.CartoDB[randomName] = function(data) { | |
callback(data); | |
delete L.CartoDB[randomName]; | |
script.parentNode.removeChild(script); | |
} | |
// Add script to page | |
var script = document.createElement('script'); | |
script.src = url + '&format=GeoJSON&callback=L.CartoDB.' + randomName; | |
script.async = true; | |
script = document.getElementsByTagName('head')[0].appendChild(script); | |
} | |
}); | |
L.cartoDB = function (url, options) { | |
return new L.CartoDB(url, options); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment