Increasingly Functional.
by Joshua Miller | on Twitter | on the web | on github

Google Maps and ClojureScript

December 2nd 2013

Tagged: clojure

I've been having a reasonably productive time getting up to speed with ClojureScript for my Harrisburg city crime tracker. One difficult bit of trial-and-error I came across is successfully compiling the Google Maps API into my app with advanced optimizations set. The Google Closure compiler munges identifiers so you need an "externs" file so it knows which names correlate to what from your CLJS code.

Here's what I have in my project.clj:

:cljsbuild {:builds
            [{:source-paths ["src/cljs"]
              :compiler
                {:output-to "resources/public/hbg-crime.js"
                 :externs ["google_maps_api_v3.js"
                           "jquery-1.9.js"
                           "foundation-datepicker.js"]
                 :optimizations :advanced}}]}

The :externs vector is a list of all the externs JS files (which are then placed in the src/cljs directory — actually I should put them in a src/cljs/externs directory for organization's sake).

The contents of google_maps_api_v3.js come from https://code.google.com/p/closure-compiler/source/browse/contrib/externs/maps/google_maps_api_v3.js.

Then, you can use the Maps API from inside your CLJS via the normal JS interop methods:

(def position (google.maps.LatLng. lat lng))

For further examples, you can refer to the hbg-crime.org source.