2013-01-31 20 views
0

我不明白爲什麼這會不會CoffeeScript的工作。我試圖簡化此示例:http://openlayers.org/dev/examples/click-handler.html的OpenLayers + Rails的3 +的CoffeeScript = a.draw不是一個函數

在視圖中,我有ID映射一個div並調用initMap()。

我得到的錯誤是:

TypeError: a.draw is not a function 

在的OpenLayers中間的lib

我在一家.js.coffeescript文件下面的代碼:

@MarkOnce = OpenLayers.Class OpenLayers.Control, 
defaultHandlerOptions: 
    'single': true 
    'double': false 
    'pixelTolerance': 0 
    'stopSingle': false 
    'stopDouble': false 

initialize: -> 
    this.handlerOptions = OpenLayers.Util.extend({}, this.defaultHandlerOptions) 
    OpenLayers.Control.prototype.initialize.apply(this, arguments) 
    this.handler = new OpenLayers.Handler.Click(this, {'click': this.mark},  this.handlerOptions) 

mark: (evt) -> 
    console.log 'mark' 
    alert('pan') 


default_marker = (lonlat) -> 
    size = new OpenLayers.Size(21,25) 
    offset = new OpenLayers.Pixel(-(size.w/2), -size.h) 
    icon = new OpenLayers.Icon('http://www.openlayers.org/dev/img/marker.png',size,offset) 
    marker = new OpenLayers.Marker(lonlat,icon) 
return marker 

    @initMap = (lon,lat,marker) -> 
    @map = load_map(lon,lat) 
    render_marker(@map,lon,lat) if marker 
    set_projection(@map,lon,lat) 
    set_callbacks(@map) 

load_map = (lon,lat) -> 
    map = new OpenLayers.Map 'map' 
    layer = new OpenLayers.Layer.OSM() 
    map.addLayer(layer) 
    map 

set_projection = (map, lon, lat) -> 
    projection = new OpenLayers.Projection("EPSG:4326") 
    point = new OpenLayers.LonLat(lon,lat) 
    console.log(point) 
    center = point.transform(projection, map.getProjectionObject()) 
    map.setCenter(point, 5) 

render_marker = (map,lon,lat) -> 
    layer_once = new OpenLayers.Layer.Markers("mark_once") 
    map.addLayer(layer_once) 

    lonlat = new OpenLayers.LonLat(lon,lat) 
    layer_once.addMarker(@default_marker(lonlat)) 
    @numMarkers++ 

set_callbacks = (map) -> 
    click = new MarkOnce() 
    map.addControl(click) 
    click.activate() 

編輯:看來,如果你更改this.defaultHandlerOptions {得出: - >警報「鍋」}它的工作原理。現在的問題是:在這個範圍內發生了什麼?這是什麼」?

EDIT2:這個abordage一個可能的問題是,每當我在地圖上縮小或放大,標記放置在地圖的中心。如何避免這種情況發生?

EDIT3:如果你到這一點,炸燬你所做的一切,去使用單張。

回答

2

我用CoffeeScript從OpenLayers.Control和OpenLayers.Handle.Click使用OpenLayers.Class創建點擊處理程序的問題就像您嘗試的那樣 - 但似乎並不需要。

請參閱Do I have a OpenLayers/jQuery conflict here? 瞭解如何非常輕鬆地在地圖構造函數的選項參數中建立eventlistener。這對我來說很好。

+0

您好,感謝您的評論,但是這是很久以前的事,我無法測試,如果現在這個問題解決了。如前所述,我最終轉向傳單。但是,既然你正在恢復這一點,你應該是對的,所以我把你的答案設定爲接受。歡迎來到SO。 – fotanus

相關問題