5

我正在尝试用 ClojureScript 重写 OpenLayers 的示例用法。

Javascript 源代码如下所示:

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);  

我已经像这样重写了这段代码:

(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 生成以下代码:

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 = {};

它不工作。那么问题在于 ClojureScript 在没有new其他东西的情况下生成代码吗?

4

1 回答 1

8

是的,您的构造函数调用需要修复:

(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)

未经测试,但你应该明白。请注意构造实例和调用实例方法之间的区别。

于 2012-05-01T14:19:44.200 回答