简体   繁体   English

触发单击小册子标记

[英]Trigger click on leaflet marker

I have a bunch of leaflet markers on the map. 我在地图上有一堆传单标记。 Each marker is held in array markers . 每个标记都保存在阵列markers The markers are created dynamically (during an ajax call). 标记是动态创建的(在ajax调用期间)。

var markers = [];
.
.
var marker = L.marker([mar.lat, mar.lng], {
  // ...build the marker...
}
marker._leaflet_id = mar.id; // give the marker an id corresponding to the id of its corresponding div
var myHoverIcon = L.icon({
  iconUrl: mar.imgUrl,
  iconSize: [40, 40],
  popupAnchor: [0, 0]
});
marker.on('click', function(e) {
  alert('Marker clicked!');
  marker.setIcon(myHoverIcon);
});
.
.
markers.push(marker);

Each marker has an id corresponding to a particular div (stored in data-mess_id on the div). 每个标记具有对应于特定div的id(存储在div上的data-mess_id )。 The plan is to change the marker's icon when its corresponding div in the feed is clicked on. 计划是在单击Feed中相应的div时更改标记的图标。

$('#feed').on('mouseover', '.message', function() {
  var cssid = $(this).attr('data-mess_id').toString();
  var baz = $.grep(markers, function(m) {
    return (m._leaflet_id == cssid);
  });
  baz[0].trigger('click');   // doesn't work 
  alert(baz[0].getLatLng()); // does work

  // this also does not work:
  var myHoverIcon = L.icon({
    iconUrl: baz[0].imgUrl,
    iconSize: [40, 40],
    popupAnchor: [0, 0]
  });
  baz[0].setIcon(myHoverIcon);

});

It's all working fine except for the final bit. 除了最后一点,它一切正常。 I just can't trigger a click event on the marker. 我只是无法触发标记上的点击事件。 I definitely have the correct marker because baz[0].getLatLng() is working. 我肯定有正确的标记,因为baz[0].getLatLng()正在工作。 But baz[0].trigger('click') doesn't work. 但是baz[0].trigger('click')不起作用。

I tried creating a new icon dynamically ( myHoverIcon ) but it doesn't work. 我尝试动态创建一个新图标( myHoverIcon ),但它不起作用。

How do I trigger a click event on the marker? 如何在标记上触发点击事件?

Is there another way to change the marker icon? 还有其他方法可以更改标记图标吗?

To emulate a mouse click, you can use the fire method (inherited from Evented.fire ) on the marker : 要模拟鼠标单击,您可以在标记上使用fire方法(继承自Evented.fire ):

marker.fire('click');

And a demo 还有一个演示

 var map = L.map('map').setView([0, 0], 12); var icon = L.icon({ iconUrl: 'http://leafletjs.com/examples/custom-icons/leaf-green.png' }); var marker = L.marker([0, 0], {icon: icon}) .addTo(map); var myHoverIcon = L.icon({ iconUrl: 'http://leafletjs.com/examples/custom-icons/leaf-red.png' }); marker.on('click', function(e) { marker.setIcon(myHoverIcon); }); document.querySelector('button').addEventListener('click', function() { marker.fire('click'); }); 
 html, body { height: 100%; margin: 0; } #map { width: 100%; height: 100%; } button {position: absolute; left:10 px; top: 70px;} 
 <link rel="stylesheet" href="https://unpkg.com/leaflet@1.2.0/dist/leaflet.css" integrity="sha512-M2wvCLH6DSRazYeZRIm1JnYyh22purTM+FDB5CsyxtQJYeKq83arPe5wgbNmcFXGqiSH2XR8dT/fJISVA1r/zQ==" crossorigin=""/> <script src="https://unpkg.com/leaflet@1.2.0/dist/leaflet.js" integrity="sha512-lInM/apFSqyy1o6s89K4iQUKg6ppXEgsVxT35HbzUupEVRh2Eu9Wdl4tHj7dZO0s1uvplcYGmt3498TtHq+log==" crossorigin=""></script> <div id='map'></div> <button>Click me</button> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM