javascript 如何单击 OpenLayers 上的标记

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11525679/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 13:31:31  来源:igfitidea点击:

How to Click the Marker on OpenLayers

javascriptjqueryopenlayers

提问by Jhonny Jr.

I just make a map using openlayer

我只是使用 openlayer 制作地图

I made a map in OpenLayers with our own homemade

我用我们自己的自制在 OpenLayers 中制作了一张地图

But what makes me confused is that I can not integrate jQuery with OpenLayers, where I create a function that is simple jQuery show / hide ()

但让我感到困惑的是,我无法将 jQuery 与 OpenLayers 集成,在那里我创建了一个简单的 jQuery 函数 show / hide ()

I tried to click on one of the marker in OpenLayers map which I have made, which has id #OL_Icon_43inside div#mapOpenLayers and I tried to do the function hide()using jquery in the <head>tag that will hide the tag outside tag #map, but that does not work for me

我试图点击我制作的 OpenLayers 地图中的一个标记,它#OL_Icon_43div#mapOpenLayers中有 id ,我尝试hide()<head>标签中使用 jquery来执行该功能,该功能将隐藏标签外的标签#map,但这对我不起作用

Can you help me please ?

你能帮我吗 ?

This is the view which I make the jquery code :

这是我制作 jquery 代码的视图:

$(document).ready(function(){
   $("#OL_Icon_43").click(function() {
     $("footer").hide();
   });
});

回答by Baylor Rae'

There's a chance that jQuery cannot find the element #OL_Icon_43when you are attempting to bind the click event. You will be better off delegating a click eventon the #mapinstead.

#OL_Icon_43当您尝试绑定单击事件时,jQuery 可能找不到该元素。您将关闭更好委托click事件#map,而不是。

$('#map').delegate('#OL_Icon_43', 'click', function() {
  $('#footer').hide();
});


Edit: It looks like OpenLayers allows you to bind events directly to your markers.

编辑:看起来 OpenLayers 允许您将事件直接绑定到您的标记

var marker = new OpenLayers.Marker(lonlat);
marker.id = "1";
marker.events.register("click", marker, function() {
  $('footer').hide();
});

You just need to make sure jQuery has loaded before OpenLayers so you can hide the footer. I would recommend moving your javascript tags to the bottom of the page before the closing </body>tag.

您只需要确保在 OpenLayers 之前加载了 jQuery,这样您就可以隐藏页脚。我建议在结束</body>标记之前将您的 javascript 标记移至页面底部。