如何在 OpenLayers 中创建简单的可拖动标记

发布于 2024-10-06 18:10:21 字数 770 浏览 5 评论 0原文

非常简单的问题,如何在 OpenLayers 地图中创建可拖动标记,并在拖动时获取纬度。从谷歌看来,你可以有可拖动的向量,但不能有可拖动的标记。

我看到了一些对 OpenLayers.Control.DragMarker 的引用,但这似乎不在“核心”库中。它在 this 示例中使用,但似乎正在使用一个自定义的 OpenLayers JS 文件,所以我不知道如何将其包含在我的代码中。

到目前为止的代码(添加一个简单的标记):

var layer = new OpenLayers.Layer.TMS( "TMS The Layer","",
    {  url: '', serviceVersion: '.', layername: '.', alpha: true,
        type: 'png', getURL: overlay_getTileURL 
    });

map.addLayers([layer]);


    var markers = new OpenLayers.Layer.Markers( "Markers" );
    map.addLayer(markers);
    var lonLat = new OpenLayers.LonLat( -100 ,100 );
    markers.addMarker(new OpenLayers.Marker(lonLat));

Very simple question, how can I create a draggable marker in an OpenLayers map, and get the latlon when its been dragged. From google, it seems that you can have draggable vectors, but not draggable markers.

I've seen some references to OpenLayers.Control.DragMarker, but this doesn't seem to be in the "core" library. It's used in this example but that seems to be using a custom OpenLayers JS file, so I have no idea how to go about including that in my code.

Code thus far (to add a simple marker):

var layer = new OpenLayers.Layer.TMS( "TMS The Layer","",
    {  url: '', serviceVersion: '.', layername: '.', alpha: true,
        type: 'png', getURL: overlay_getTileURL 
    });

map.addLayers([layer]);


    var markers = new OpenLayers.Layer.Markers( "Markers" );
    map.addLayer(markers);
    var lonLat = new OpenLayers.LonLat( -100 ,100 );
    markers.addMarker(new OpenLayers.Marker(lonLat));

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

暮倦 2024-10-13 18:10:22

我已经做到了,但现在找不到该代码。看一下这个 OpenLayers 示例:

拖动功能

I've done that, but I can't find that code now. Take a look at this OpenLayers Example:

Drag Feature

青芜 2024-10-13 18:10:22

你能用特征来改变标记吗?看这个链接

Can you change the markers with features? look this link

只是一片海 2024-10-13 18:10:22

我在这个问题上遇到了一些困难,直到我深入研究文档并意识到在 2022 年做到这一点非常容易:

const translate = new Translate({
   features: new Collection([feature]),
})

translate.on('translateend', e => {
   let lonLat = toLonLat(e.coordinate)
})

map.addInteraction(translate)

I've struggled with that a bit, until I dag into the documentation and realized that in 2022 it is very easy to do that:

const translate = new Translate({
   features: new Collection([feature]),
})

translate.on('translateend', e => {
   let lonLat = toLonLat(e.coordinate)
})

map.addInteraction(translate)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文