7

I have the following code:

    function mapCreate(lon,lat){
    map = new OpenLayers.Map("map1");
    var osm = new OpenLayers.Layer.OSM();
    vectors = new OpenLayers.Layer.Vector("Vector Layer");
    map.addLayer(osm);
    var center = new OpenLayers.LonLat(lon,lat).transform(
        new OpenLayers.Projection("EPSG:4326"),
        map.getProjectionObject()
    );

    point = new OpenLayers.Geometry.Point(center.lon,center.lat);
    vectors.addFeatures([new OpenLayers.Feature.Vector(point)]);
    drag = new OpenLayers.Control.DragFeature(vectors);
    map.addControl(drag);
    drag.activate();
    map.setCenter(center, 15);
    map.addLayer(vectors);
    point.events.register('moveend',point, function(evt){
        alert('hello');
    });

}

The part that does not work is:

        point.events.register('moveend',point, function(evt){
        alert('hello');
    });

Can anyone help me with creating an event listener for a point in OpenLayers?

1 Answer 1

6

You should use onComplete property of OpenLayers.Control.DragFeature, for example:

drag = new OpenLayers.Control.DragFeature(vectors, {
    autoActivate: true,
    onComplete: function() {alert('hello')}
});

Here is my online example: DragFeature.

1
  • Thanks, this helped me out too. Question: It strikes me that having to pass an empty function to a control to make it work is, well...kind of dumb. What is going on here that leads to this?
    – bshender
    Commented May 2, 2013 at 13:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.