首页 > 解决方案 > 单击Angular Mapbox获取坐标

问题描述

当我在 Angular 中单击我的地图时,我试图获取坐标,但事件返回 undefined。这是我的代码

HTML

<div id="map" (click)="getCoords($event)"></div>

TS

  getCoords(e){ 
      console.log(e.lngLat); // undefined
      }

标签: angularmapbox

解决方案


Mapbox 鼠标指针坐标是每个MapMouseEvent事件的参数参数的一部分。您只需通过Eventedsintax 声明事件侦听器。在这里,您可以捕获最简单的代码段:

// The `click` event is an example of a `MapMouseEvent`.
// Set up an event listener on the map.
map.on('click', function(e) {
    // The event object (e) contains information like the
    // coordinates of the point on the map that was clicked.
    console.log('A click event has occurred at ' + e.lngLat);
});

推荐阅读