我想在 OpenLayers 地图上为位置创建一个可拖动的图标。
使用下面的代码,我可以在地图上放置一个图标(使用 PNG 图像),但它不可拖动。我怎样才能做到这一点?
<html>
<body>
<div id="mapdiv"></div>
<script src="http://www.openlayers.org/api/OpenLayers.js"></script>
<script>
map = new OpenLayers.Map("mapdiv");
map.addLayer(new OpenLayers.Layer.OSM());
var lonLat = new OpenLayers.LonLat( 72.91152, 19.11186 )
.transform(
new OpenLayers.Projection("EPSG:4326"), // transform from WGS 1984
map.getProjectionObject() // to Spherical Mercator Projection
);
var zoom=16;
var osm = new OpenLayers.Layer.OSM();
var vectors = new OpenLayers.Layer.Vector("Vector Layer");
map.addLayers([osm, vectors]);
var markers = new OpenLayers.Layer.Markers( "Markers" );
map.addLayer(markers);
var size = new OpenLayers.Size(25,37);
var offset = new OpenLayers.Pixel(-(size.w/2), -size.h);
mymarker = new OpenLayers.Icon('marker-icon.png', size, offset);
markers.addMarker(new OpenLayers.Marker(lonLat, mymarker));
var point = new OpenLayers.Geometry.Point(lonLat.lon,lonLat.lat);
vectors.addFeatures([new OpenLayers.Feature.Vector(point)]);
drag = new OpenLayers.Control.DragFeature(vectors, {autoActivate: true});
map.addControl(drag);
map.setCenter (lonLat, zoom);
</script>
</body></html>