trying to write simple binding to google maps

This commit is contained in:
Danny Yoo 2012-03-19 18:22:31 -04:00
parent beb2c02e31
commit 7fbb36e4b1
2 changed files with 62 additions and 13 deletions

View File

@ -0,0 +1,44 @@
#lang planet dyoo/whalesong
;; A simple binding to Google Maps.
(require (planet dyoo/whalesong/js))
;; initialize-google-maps-api!: string boolean -> void
;; Dynamically loads the Google Maps API.
(define raw-initialize-google-maps-api!
(js-async-function->procedure
#<<EOF
function(success, fail, key, sensor) {
var callbackName = 'afterGoogleMapsInitialized' + plt.runtime.makeRandomNonce();
window[callbackName] = function() {
delete(window[callbackName]);
// At this point, we know the API has been instantiated ok.
success();
};
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "http://maps.googleapis.com/maps/api/js?key="
+ encodeURIComponent(key) + "&sensor=" + (sensor ? 'true' : 'false')
+ "&callback=" + encodeURIComponent(callbackName);
document.body.appendChild(script);
}
EOF
))
(define (initialize-google-maps-api! key sensor)
(unless (string? key)
(raise-type-error 'initialize-google-maps-api! "string" 0 key))
(unless (boolean? sensor)
(raise-type-error 'initialize-google-maps-api! "boolean" 1 sensor))
(void (raw-initialize-google-maps-api! key sensor)))
;; This is dyoo's API key.
(printf "Loading google maps api\n");
(initialize-google-maps-api! "AIzaSyCRKQNI_nbyyN1286cssEy3taKj5IZcHN8" #t)
(printf "google maps api loaded\n")

View File

@ -163,6 +163,20 @@
// makeRandomNonce: -> string
// Creates a randomly-generated nonce.
var makeRandomNonce = function() {
var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
var LEN = 32;
var result = [];
var i;
for (i = 0; i < LEN; i++) {
result.push(chars.charAt(Math.floor(Math.random() * chars.length)));
}
return result.join('');
};
//////////////////////////////////////////////////////////////////////
// Exclusive Locks. Even though JavaScript is a single-threaded
@ -175,23 +189,11 @@
this.waiters = [];
};
// makeRandomNonce: -> string
// Creates a randomly-generated nonce.
ExclusiveLock.makeRandomNonce = function() {
var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
var LEN = 32;
var result = [];
var i;
for (i = 0; i < LEN; i++) {
result.push(chars.charAt(Math.floor(Math.random() * chars.length)));
}
return result.join('');
};
ExclusiveLock.prototype.acquire = function(id, onAcquire) {
var that = this;
if (!id) {
id = ExclusiveLock.makeRandomNonce();
id = makeRandomNonce();
}
this.alreadyReleased = false;
@ -1268,4 +1270,7 @@
exports['checkedCdr'] = checkedCdr;
exports['checkedVectorRef'] = checkedVectorRef;
exports['checkedVectorSet'] = checkedVectorSet;
exports['makeRandomNonce'] = makeRandomNonce;
}(this.plt, this.plt.baselib));