openlayers 简单地将鼠标悬停在标记上
听起来很简单,但我找不到任何新手教程:任何人都可以给我一个简单的示例,如何在 OpenLayers 中创建(矢量)标记,在鼠标悬停时打开信息窗口,甚至在鼠标移出时关闭它?
我发现其中部分内容已得到解释,但并非全部......
It sounds so simple but I can't find any newbie tutorial: Could anybody give me a simple example how I create (vektor)markers in OpenLayers that open an infowindow on mouseover and even close it on mouseout?
I found parts of this explained but not all of it....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
作为如何执行此操作的简单示例,您需要执行以下操作:
创建一个矢量图层来包含标记并将其添加到地图中:
创建标记并将其添加到地图中:
为您的标记创建一个选择控件层,并注册一个函数来在用户将鼠标悬停在标记上时构建弹出窗口:
构建弹出窗口:
For a simple example of how to do this, you need to do a couple of things:
Create a vector layer to contain your markers and add it to the map:
Create your marker and add it to the map:
Create a select control for your layer, and register a function to build your popup when the user hovers over your marker:
Build your popup:
您可以使用
marker
和popup
,如下所示。You can use
marker
andpopup
, as below.这对我有用。最终很简单,但花了一些时间:
请注意鼠标移出事件的 4000 微秒延迟 - 也许可能更短,具体取决于您的使用情况。
This works for me. Ended up being simple, but took a while:
Note the 4000 microsecond delay on the mouse out event - maybe could be shorter, depends on your use.
您需要使用 selectControl 来创建弹出窗口。要使其响应悬停而不是单击,请在构造函数中设置悬停:true。
You need to use a selectControl to create your popup. To make it respond to hover instead of click set hover:true in the constructor.
我使用一个函数来添加它,这是一个简化版本。请注意,您设置详细信息并调用底部的函数。另请注意,您不能在多层上有多个选择器 - 我认为只有最后添加的选择器才有效,因此如果您想在一层中使用多个功能,则必须对其进行调整:
I use a function to add it, here's a simplified version. Note that you set the details and call the function at the bottom. Also note that you can't have multiple selectors on multiple layers - I think only the last one added will work, so you'll have to tweak it if you want multiple features in one layer:
我用 ol 5.2 example“自定义交互”编写了一个工作示例
openlayers.org/en/latest/examples/select-features.html
因此,您将功能添加到图层并将其添加到地图中,然后创建一个像这样的新交互
它指定它将在悬停时选择一个功能(指针移动)
然后,将其添加到地图中,并关联当交互选择某个功能时(即,当您将鼠标悬停在某个功能上时)应调用的函数。
并且您声明显示信息窗口的函数
您应该注意,该事件将返回(在本例中)一个对象,当您将鼠标悬停在其中时,您可以在“selected”属性中找到选定的功能。在这种情况下,当您将鼠标悬停时,“取消选择”属性中将提供相同的对象,直到您选择一个新对象为止
I wrote a working exemple of this with ol 5.2 exemple "Custom Interaction"
openlayers.org/en/latest/examples/select-features.html
So you add features to a layer and add it to the map and then you create a new Interaction like this
It specifies that it will select a feature on hover (pointermove)
Then you add it to your map and associate the function that should be called when the interaction selects a feature (i.e. when you hovers over one).
And you declare your function that shows the info window
You should note that the event will return (in this case) an object where you can find the selected feature in the 'selected' attribute when you hover in it. When you hover out, in this case, the same object will be available in the 'deselected' attribute until you select a new one
这个来自用户列表的示例非常有助于展示如何将悬停和单击事件分配给矢量图层。
This example from a userlist was very helpful in showing how to have both hover and click events assigned to a vector layer.
听起来您想查看“OpenLayers.Popup()”,
此示例在绘制多边形后显示一个弹出窗口,然后单击它,但您应该能够更改它以响应悬停。
http://openlayers.org/dev/examples/select-feature-openpopup.html
It sounds like you want to look at "OpenLayers.Popup()"
this example shows a popup after you draw a polygon, then click on it, but you should be able to change it to respond to hover instead.
http://openlayers.org/dev/examples/select-feature-openpopup.html