#lang planet dyoo/whalesong
(require (planet dyoo/whalesong/js)
(planet dyoo/whalesong/js/world))
(provide initialize-google-maps-api!
make-dom-and-map
make-on-map-click)
(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(plt.runtime.VOID) }
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 raw-make-map-dom-and-map
(js-async-function->procedure
#<<EOF
function(success, fail, lat, lng) {
var myOptions = {
center: new google.maps.LatLng(lat, lng),
zoom: 8,
mapTypeId: google.maps.MapTypeId.ROADMAP
} var domElement = document.createElement('div') domElement.style.width = "100%" domElement.style.height = "200px" var map = new google.maps.Map(domElement, myOptions) success(domElement, map)}
EOF
))
(define (raw-make-on-map-click a-gmap)
(define raw-setup
(js-function->procedure #<<EOF
function(map, callback) {
var mapsListener =
google.maps.event.addListener(map, 'click', function(event) {
callback(plt.runtime.makeFloat(event.latLng.lat()),
plt.runtime.makeFloat(event.latLng.lng())) }) return mapsListener}
EOF
))
(define raw-shutdown
(js-function->procedure #<<EOF
function(gmap, mapsListener) {
google.maps.event.removeListener(gmap, mapsListener)}
EOF
))
(define (setup callback)
(raw-setup a-gmap callback))
(define (shutdown setup-data)
(raw-shutdown a-gmap setup-data))
(make-world-event-handler setup shutdown))
(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))
(raw-initialize-google-maps-api! key sensor))
(define (make-dom-and-map lat lng)
(unless (real? lat)
(raise-type-error 'make-dom-and-map "real" 0 lat))
(unless (real? lng)
(raise-type-error 'make-dom-and-map "real" 1 lng))
(raw-make-map-dom-and-map (number->js-number lat)
(number->js-number lng)))
(define make-on-map-click raw-make-on-map-click)