ClojureScript + OpenLayers
-
04-06-2021 - |
Question
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?
Solution
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.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow