我是@ asymmetrik / ngx-leaflet和Angular的新手,所以这可能只是一个新手问题……
我有一个使用@asymmetrik/ngx-leaflet-tutorial-ngcli的Angular.io(v5)项目
现在我想得到我在地图上点击的点的坐标.根据Issue #51 get coordinates on click?,我补充说:
map.on('click',() => { console.log(e.latlng); });
至:
onMapReady(map: Map) { map.fitBounds(this.route.getBounds(),{ padding: point(24,24),maxZoom: 12,animate: true }); map.on('click',() => { console.log(e.latlng); }); }
这给了我一个运行时错误:
找不到名字’e’.
哪种对我有意义.所以,我将代码更改为:
map.on(‘click’,(e)=> {console.log(e.latlng);});
但这也给了我一个错误:’LeafletEvent’类型中不存在属性’latlng’
当我把e放到控制台console.log(e)时,我可以看到latlng-Property存在…
为什么我不能用e.latlng访问坐标?
我的项目正在使用:
“@ angular / cli”:“1.4.7”,
“@ asymmetrik / ngx-leaflet”:“^ 2.5.1”,
“@ types / leaflet”:“^ 1.2.0”,
“传单”:“^ 1.2.0”,
解决方法
编译器推断事件类型是
LeafletEvent,它没有latlng属性.这就是你得到这个错误的原因.
Leaflet文档表明此事件实际上是LeafletMouseEvent类型,它扩展了LeafletEvent.因此,您可以转换事件以获取对LeafletMouseEvent属性的访问权限(如下所示:
map.on('click',(<LeafletMouseEvent>e) => { console.log(e.latlng); });