Javascript 传单-鼠标单击时精确的标记坐标
我正在用传单地图进行角度项目,利用标记。我需要鼠标点击得到标记坐标。在我的项目草稿中,我以以下方式预定义了标记:Javascript 传单-鼠标单击时精确的标记坐标,javascript,angular,leaflet,openstreetmap,Javascript,Angular,Leaflet,Openstreetmap,我正在用传单地图进行角度项目,利用标记。我需要鼠标点击得到标记坐标。在我的项目草稿中,我以以下方式预定义了标记: location1 = marker([ 50.0647, 19.9450 ], { title: 'City 1', icon: icon({ iconSize: [ 25, 41 ], iconAnchor: [ 13, 41 ], iconUrl: 'leaflet/marker-icon.png', icon
location1 = marker([ 50.0647, 19.9450 ], {
title: 'City 1',
icon: icon({
iconSize: [ 25, 41 ],
iconAnchor: [ 13, 41 ],
iconUrl: 'leaflet/marker-icon.png',
iconRetinaUrl: 'leaflet/marker-icon-2x.png',
shadowUrl: 'leaflet/marker-shadow.png',
})
});
然后,我以这种方式定义基础层和覆盖层:
layersControl = {
baseLayers: {
'Street Maps': this.streetMaps,
},
overlays: {
'City 1': this.location1,
'City 2': this.location2,
'City 3': this.location3,
}
};
我试图使用onmaplick获取标记坐标,但它不会返回精确的值
onMapClick (e: LeafletMouseEvent) {
// returns the coordinates of mouse click, not marker
console.log (e.latlng.lat, e.latlng.lng);
// returns the presice coordinates and fake ID (title) but layer ID of 2 is hardcoded
console.log(e.target.options.layers[2]._latlng.lng);
console.log(e.target.options.layers[2]._latlng.lng);
console.log(e.target.options.layers[2].options.title);
}
在上面的代码中,我可以提取标记坐标,但图层ID是硬编码的。
我很难找到通过鼠标点击读取图层ID的方法。
有什么办法可以解决吗?将事件附加到标记上,而不是地图上。