Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/392.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在地图框中单击以获取标记的ID_Javascript_Mapbox_Mapbox Gl Js - Fatal编程技术网

Javascript 在地图框中单击以获取标记的ID

Javascript 在地图框中单击以获取标记的ID,javascript,mapbox,mapbox-gl-js,Javascript,Mapbox,Mapbox Gl Js,我有一个角度视图,其中我从api中的一些点获取数据,我使用一种方法根据api提供的坐标放置标记。除了坐标,数据还有一个ID。 当我点击标记时,除了向我显示以某种方式获取ID并将其保存在变量中的信息外,我还需要它,以便能够使用该特定点执行函数。到目前为止,我有这个 map: Mapboxgl.Map; // THE MAP marker: Mapboxgl.Marker; // THE MARKER // THE METHOD TO CREATE THE MARKERS ON THE MAP cr

我有一个角度视图,其中我从api中的一些点获取数据,我使用一种方法根据api提供的坐标放置标记。除了坐标,数据还有一个ID。 当我点击标记时,除了向我显示以某种方式获取ID并将其保存在变量中的信息外,我还需要它,以便能够使用该特定点执行函数。到目前为止,我有这个

map: Mapboxgl.Map; // THE MAP
marker: Mapboxgl.Marker; // THE MARKER
// THE METHOD TO CREATE THE MARKERS ON THE MAP
creteGeoJSON(data) {
        data.forEach((element) => {
            const el: HTMLElement = document.createElement('div');
            el.className = 'marker';
            el.style.backgroundImage = 'url(../../../../assets/img/icon.png)';
            el.style.width = '30px';
            el.style.height = '30px';
            el.style.cursor = 'pointer';
            el.style.backgroundSize = 'cover';
            this.marker = new Mapboxgl.Marker(el)
              .setLngLat(
                element.coor
                  .split(',')
                  .reverse()
                  .map((x) => +x)
              )
              .setPopup(
                new Mapboxgl.Popup({ offset: 25 }) // add popups
                  .setHTML(
                    `<h2>ID: ${element.id} </h2>`
                  )
              )
              .addTo(this.map);
            this.currentMarkers.push(this.marker);
        });
 }

// THE FUNCTION TO GET THE LAT AND LONG
getCoords(e) {
    this.map.getCanvas().style.cursor = 'pointer';
    this.map.on('click', (e) => {
      const lat = e.lngLat.lat;
      const lng = e.lngLat.lng;
      // GET SOME ID 
    });
}
map:Mapboxgl.map;//地图
标记:Mapboxgl.marker;//记号笔
//在地图上创建标记的方法
creteGeoJSON(数据){
data.forEach((元素)=>{
const el:HTMLElement=document.createElement('div');
el.className='marker';
el.style.backgroundImage='url(../../../../../assets/img/icon.png)';
el.style.width='30px';
el.style.height='30px';
el.style.cursor='pointer';
el.style.backgroundSize=‘封面’;
this.marker=新的Mapboxgl.marker(el)
setLngLat先生(
元素库
.split(“,”)
.reverse()
.map((x)=>+x)
)
.setPopup(
新建Mapboxgl.Popup({offset:25})//添加弹出窗口
.setHTML(
`ID:${element.ID}`
)
)
.addTo(此.map);
this.currentmarks.push(this.marker);
});
}
//获取LAT和LONG的函数
getCoords(e){
this.map.getCanvas().style.cursor='pointer';
this.map.on('click',(e)=>{
常数lat=e.lngLat.lat;
const lng=e.lngLat.lng;
//拿些身份证
});
}

如果希望在用户单击标记时执行操作,则应将单击事件添加到标记,而不是地图

标记包含HTML元素,因此您只需执行以下操作:

marker=新的Mapboxgl.marker(el)
//...
.addTo(地图);
el.addEventListener('单击',()=>{
//在这里,您可以访问包含数据的'element'对象
});