Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ClojureScript + OpenLayers

I'm trying to rewrite example usage of OpenLayers with ClojureScript.

Javascript source code looks like this:

var map, layer;
map = new OpenLayers.Map('map');
layer = new OpenLayers.Layer.OSM("Simple OSM Map");
map.addLayer(layer);

var projection = new OpenLayers.Projection("EPSG:4326");
var center = new OpenLayers.LonLat(-71.147, 42.472).transform(projection, map.getProjectionObject());
map.setCenter(center, 12);  

I've rewrote this code like this:

(ns hello.map)
  (def mapp (.Map js/OpenLayers "map"))
  (def layer (.Layer.OSM js/OpenLayers "Simple OSM Map"))
  (.addLayer mapp layer)

  (def projection (.Projection js/OpenLayers "EPSG:4326"))
  (def center (.Transform (.LonLat js/OpenLayers -71.147 42.472) projection (.getProjectionObject mapp)))
  (.setCenter mapp center 12)

Lein generated following code:

var hello = {map:{}};
hello.map.mapp = OpenLayers.Map("map");
hello.map.layer = OpenLayers.Layer.OSM("Simple OSM Map");
hello.map.mapp.addLayer(hello.map.layer);
hello.map.projection = OpenLayers.Projection("EPSG:4326");
hello.map.center = OpenLayers.LonLat(-71.147, 42.472).Transform(hello.map.projection, hello.map.mapp.getProjectionObject());
hello.map.mapp.setCenter(hello.map.center, 12);
hello.hello = {};

And it's not working. So is problem in that ClojureScript generates code without new or maybe something else?

like image 409
Stepan Kuzmin Avatar asked Apr 27 '12 10:04

Stepan Kuzmin


1 Answers

Yes your constructor calls need fixing:

(ns hello.map)

(def mapp (js/OpenLayers.Map. "map"))
(def layer (js/OpenLayers.Layer.OSM. "Simple OSM Map"))
(.addLayer mapp layer)

(def projection (js/OpenLayers.Projection. "EPSG:4326"))
(def center (.transform (js/OpenLayers.LonLat. -71.147 42.472)
               projection (.getProjectionObject mapp)))
(.setCenter mapp center 12)

untested but you should get the idea. Note the difference between constructing instances and calling methods on instances.

like image 88
dnolen Avatar answered Nov 07 '22 22:11

dnolen