views:

227

answers:

0

I'm using OpenLayers in a Rails app - through the MapLayers plugin. The examples for adding controls look easy - very similar to the example code on the OpenLayers.Control class -

@map = MapLayers::Map.new("map") do |map, page|
  page << map.add_layer(MapLayers::GOOGLE)
  page << map.add_layer(MapLayers::YAHOO_HYBRID)

  page << map.add_control(Control::LayerSwitcher.new)    
  page << map.add_control(Control::Permalink.new('permalink'))    
  page << map.add_control(Control::MousePosition.new)

  page << map.zoom_to_max_extent()
end

Now, I want to add a ClickHandler to catch when a user clicks on the map to populate some fields for latitude/longitude in my page. There's a ClickHandler example (view source of the linked page) that shows how to do this in JavaScript. However, in the example, it's not attaching the ClickHandler to a Control through the Control.handler property- it's creating a custom control and initializing handler to ClickHandler in the constructor.

I've tried this code -

@map = MapLayers::Map.new("map") do |map, page|
  # ...
  mouseControl = Control::MousePosition.new
  clickHandler = Handler::Click.new(mouseControl, 
      {:click=>'onMapClick'}, # calls a method declared in the header
      {:single=>true})
  mouseControl.handler = clickHandler
  page << map.add_control(mouseControl)
  # ...
end

but no dice, I just get

map.addControl(new OpenLayers.Control.MousePosition());

in the page output.

I've also thought about trying to create the ClickHandler first and pass it into the MousePosition Control's constructor - but I can't because the Handler constructor requires a reference to a Control as the first parameter.

Is there an easy way to add a Handler to a Control through MapLayers? Or do I have to do everything through OpenLayers in JavaScript directly?