-4

我想在 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>
4

1 回答 1

2

DragFeature控件适用于矢量特征。如果要拖动图标,则需要将该功能设置为图标。像这样的东西,但无法访问您的图像,我无法确定您需要的大小和偏移量

vectors.addFeatures([new OpenLayers.Feature.Vector(point, null, {
    externalGraphic: 'marker-icon.png',
    graphicWidth: 25,
    graphicYOffset: -30
})]);
于 2019-08-07T11:45:44.643 回答