最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Dragging markers with Ordnance SurveyOpenlayers api - Stack Overflow

programmeradmin4浏览0评论

I've created a site link text using the api produced by the Ordnance Survey (I think this is called OpenSpace) which is based on OpenLayers. I've got it so you can click on the map to add a marker and I want to then be able to click on the markers and drag it around the map. Is there a simple way to do this using either the OpenSpace or OpenLayers apis.

I've created a site link text using the api produced by the Ordnance Survey (I think this is called OpenSpace) which is based on OpenLayers. I've got it so you can click on the map to add a marker and I want to then be able to click on the markers and drag it around the map. Is there a simple way to do this using either the OpenSpace or OpenLayers apis.

Share Improve this question asked May 19, 2009 at 19:49 ChrisChris 2505 silver badges16 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

The OpenLayers api allows you to add Markers and Features to the map. If you add Features rather than Markers you can make them draggable by adding the following code.

var vectorLayer = new OpenLayers.Layer.Vector("Vector Layer");
var osMap = new OpenSpace.Map('map');
osMap.addLayer(vectorLayer);

var modifyFeaturesControl = new OpenLayers.Control.ModifyFeature(vectorLayer);
modifyFeaturesControl.mode = OpenLayers.Control.ModifyFeature.RESHAPE;
osMap.addControl(modifyFeaturesControl);
modifyFeaturesControl.activate();

This will allow you to drag features around a map. If you want to add custom behavior when feature's are dragged you can register listeners on the vectorLayer. For example to register a listener when features are modifed (i.e. dragged and released) you need to use the following code.

vectorLayer.events.register('featuremodified', vectorLayer, function(feature) {
   //custom behavior
});

For a full list of the events that can be listened to see the OpenLayers api doc OpenLayers api doc

As easier way to do this is by using the openlayer drag control, which takes a vector layer as a target.

Assuming a vector layer containing icons (this is prefered to markers, which the developers of OL discourage using), called vectors, you can simply do:

var drag=new OpenLayers.Control.DragFeature(vectors);

map.addControl(drag);
drag.activate();

The other advantage of using the drag control is that you can hook into various callbacks, which return the feature and pixel position, such as onStart and onDrag. eg,

var drag=new OpenLayers.Control.DragFeature(vectors,{    
     'onDrag':function(feature, pixel){
           console.log(pixel.x);//this can be used to do something else, such as move another feature
     }
});

See http://trac.osgeo/openlayers/browser/trunk/openlayers/lib/OpenLayers/Control/DragFeature.js for more details.

发布评论

评论列表(0)

  1. 暂无评论