Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenLayers event.register not registering

Tags:

openlayers

Ok I am a noob to OpenLayers. The problem that I have is this...

In my javascript, after initializing the map, adding the base OSM layer and centering, my code does an ajax lookup of points for the markers to add to the map, all markers are in groups, so it creates a new layer for every group and adds the markers to the group layer. But, before adding the marker to the layer it does an event register of mousedown with a simple alert function.

The problem here is that when I go to click on a marker, the cursor just turns to a hand like it thinks I want to drag the map. It's like there's some other layer on top that's preventing the click. I tried console logging instead of the alert just to make sure but it never actually triggers the event click. It's hard to do a code sample that will display the full picture of the code, but here's a snippet:

function createMarker(lat, lon) {
    var icon = new OpenLayers.Icon('/mapicon/icon-b.png', new OpenLayers.Size(12, 20), new OpenLayers.Pixel(-6, -10)); // this is a custom image
    lonlat = new OpenLayers.LonLat(lon, lat);
    var marker = new OpenLayers.Marker(lonlat, icon.clone());
    return marker;
}

marker = createMarker(lat,lon,'Example Title');
marker.events.register('mousedown', pin, function(evt) { alert('help!'); OpenLayers.Event.stop(evt); });

I am hitting a wall and if I weren't bald, I'd be pulling my hair out. Anyone have any ideas?

like image 645
Andrew Christensen Avatar asked Feb 23 '11 23:02

Andrew Christensen


Video Answer


4 Answers

Thanks for the help all. The problem is (and the OL documentation is HORRIBLY pathetic so it doesn't state this anywhere) that it has to do with the order in which you create the layers. In my code, I loop through an array of layer names and I was creating a marker layer and a vector layer for each name. I tried it with creating the marker layer first and then creating the vector layer first and it had the same click crippling effect. So I tried looping through once to create all of the vector layers first. The loop through again and create the marker layers and that worked.

OL is a great mapping system. It's just a shame that they have such poor documentation. The samples are nearly useless. If OL is such a community driven thing, why is there no OL forums? I had to come here because it seems to be the only place where OL users do any kind of posting.

like image 166
Andrew Christensen Avatar answered Dec 04 '22 03:12

Andrew Christensen


Try this:

function createMarker(lat, lon) {
    var icon = new OpenLayers.Icon('/mapicon/icon-b.png', new OpenLayers.Size(12, 20), new OpenLayers.Pixel(-6, -10));
    lonlat = new OpenLayers.LonLat(lon, lat);
    var marker = new OpenLayers.Marker(lonlat, icon.clone());
    return marker;
}

marker = createMarker(lat,lon,'Example Title');
marker.events.register('mousedown', marker, function(evt) {
   alert('help!');
});

The second parameter of register function should be object that you want to bind the event. I always have in my mind the example of mouseover event for OpenLayers.Map object:

map = new OpenLayers.Map('divmap');
map.events.register('mouseover', map, function(evt) {
   // Do stuff
});

I hope it help you. Happy codding!

like image 24
Fran Verona Avatar answered Dec 04 '22 03:12

Fran Verona


Maybe you should look into layer events instead. I assume that you are using a vector layer for your points? In that case, look at the Vector layer documentation about event handling.

Having different layers that should receive the mouse events should work.

Also, take a look at this sample on the OL page.

Sorry if I'm stating the obvious, and you've been trying this already.

EDIT:

Realized from your answer that you might want to take a look this question that I asked and later answered with a OL hack. Maybe you can draw something from it.

like image 43
Niklas Wulff Avatar answered Dec 04 '22 03:12

Niklas Wulff


I have the same problem and I think I found the solution:

Look at this: Capture feature events on different layers

The problem seems to be that you cannot listen different events on different layers at the same time, you can do only in the active layer.

PS: I didn't try it yet...

like image 37
mpccolorado Avatar answered Dec 04 '22 03:12

mpccolorado