Search code examples
openlayers

OpenLayers .containsPoint after pan


I've seem to have hit a bug or i have overlooked something.

I written some code that enumerates through all the vector features on a OpenLayers Vector layer - to check if the mouse is inside a vector feature - if so then it displays some info based on the feature.

I had to write my own methods to do this because the existing OpenLayers Controls( select etc) stop after finding a feature under the mouse, and i the possibility of several features being stacked on top of one another.

My problem is that the .containsPoint method seems to be using coords from before a 'pan'. After zooming in or out the geometry seems to be in the right place and .containsPoint is works correctly when I wave the mouse over the map.

Do I need to do something after the map has been panned to update something( feature's geometry)


Solution

  • I realize this question has almost had it's first birthday, and it may not be valid anymore, but I'll give a shot at answering it anyway.

    OpenLayers.Control.DragPan makes a call to the map when you are done panning. If you check the OpenLayers.Map pan function, you will see this comment inside:

    // only call setCenter if not dragging or there has been a change
    

    The following logic in the function reflects this comment.

    So yes, every time you are done panning you need to do something like:

    dragpan.handler.up = function(evt)
    {
        dx = dragpan.handler.last.x - evt.x;
        dy = dragpan.handler.last.y - evt.y;
        var centerPx = map.getViewPortPxFromLonLat(map.getCenter());
        var newCenterPx = centerPx.add(dx, dy);
        var newCenterLonLat = map.getLonLatFromViewPortPx(newCenterPx);
        map.setCenter(map.getCenter() + dragpan.handler.xy);
    }
    

    ...and things should magically work for you.