gpt4 book ai didi

ClojureScript + OpenLayers

转载 作者:行者123 更新时间:2023-12-05 00:34:43 24 4
gpt4 key购买 nike

我正在尝试使用 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或者别的什么?

最佳答案

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

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

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

关于ClojureScript + OpenLayers,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10349168/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com