OpenLayers + Rails 3 + CoffeeScript = a.draw 不是一个函数

OpenLayers + Rails 3 + CoffeeScript = a.draw is not a function

本文关键字:一个 函数 draw Rails CoffeeScript OpenLayers      更新时间:2023-09-26

我不明白为什么这不适用于Coffeescript。我试图简化这个例子:http://openlayers.org/dev/examples/click-handler.html

在视图中,我有一个带有 ID map 的div 并调用 initMap()。

我得到的错误是:

 TypeError: a.draw is not a function

在开放层库的中间

我在 .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()

编辑:似乎如果您更改 {draw: -> 警报"pan"} 的 this.defaultHandlerOptions,它就可以工作了。 现在的问题是:在这个范围内发生了什么?"这"是什么?

编辑2:这种异常的一个可能问题是,每当我在地图中缩小或缩小时,标记都会放置在地图的中心。如何避免这种情况发生?

编辑3:如果你到了这一步,炸毁你正在做的任何事情,然后去使用传单。

我在 CoffeeScript 使用 OpenLayers.Control 和 OpenLayers.Handle.Click 使用 OpenLayers.Class 创建点击处理程序时遇到了同样的问题,就像你尝试的那样 - 但似乎不需要。

请参阅此处是否存在 OpenLayers/jQuery 冲突?了解如何非常轻松地在映射构造函数的选项参数中建立事件侦听器。这对我来说很好用。