clickevent的坐标 [英] Coordinates on clickevent

查看:149
本文介绍了clickevent的坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是@ asymmetrik/ngx-leaflet和Angular的新手,所以这可能只是新手发布的问题...

I'm new to @asymmetrik/ngx-leaflet and Angular in general, so this is maybe just a newbie-issue...

我有一个使用 @ asymmetrik/ngx- leaflet-tutorial-ngcli

现在,我想获取我在地图上单击的点的坐标.根据问题#51获得点击坐标?,我添加了这个内容: /p>

Now I would like to get the coordinates of the point I clicked on the map. According to Issue #51 get coordinates on click?, I added this:

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); });
}

这给了我一个运行时错误: Cannot find name 'e'.

that gives me a runtime error: Cannot find name 'e'.

对我来说这是有意义的.因此,我将代码更改为:

Which kind of makes sense to me. So, I changed the code to:

map.on('click', (e) => { console.log(e.latlng); });

但这也给我一个错误:Property 'latlng' does not exist on type 'LeafletEvent'

But this gives me an error too: Property 'latlng' does not exist on type 'LeafletEvent'

当我将e放置到控制台console.log(e)时,我可以看到latlng-Property存在... 为什么我不能使用e.latlng访问坐标?

When I just put e to the console console.log(e) I can see the latlng-Property exists... Why can't I access the coordinates with e.latlng?

我的项目正在使用:

"@ angular/cli":"1.4.7",
"@ asymmetrik/ngx-leaflet":"^ 2.5.1",
"@ types/leaflet":"^ 1.2.0",
传单":"^ 1.2.0",

"@angular/cli": "1.4.7",
"@asymmetrik/ngx-leaflet": "^2.5.1",
"@types/leaflet": "^1.2.0",
"leaflet": "^1.2.0",

推荐答案

编译器推断事件类型为

The compiler is inferring that the event type is LeafletEvent, which doesn't have the latlng property. That's why you're getting this error.

Leaflet文档表明此事件实际上是 LeafletMouseEvent ,它扩展了LeafletEvent.因此,您可以投射事件以访问LeafletMouseEvent的属性(如下所示:

The Leaflet docs indicate that this event is actually of type LeafletMouseEvent, which extends LeafletEvent. So, you can cast the event to gain access to the properties of LeafletMouseEvent (as demonstrated below:

map.on('click', (<LeafletMouseEvent>e) => {
    console.log(e.latlng);
});

这篇关于clickevent的坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆