mapbox创建自定义marker图标绑定弹框展示信息

/**
         * 创建自定义marker图标绑定弹框
        */
       let carMarker = [];//存放marker图标
       for(let m= 0;m;m++){
            let el = document.createElement("div");
            el.className = "marker-car"+m;
            el.setAttribute("id_",gpsInfoList[m].VehicleId);
            el.setAttribute("index_",gpsInfoList[m].VehicleId);
            el.setAttribute("tel",gpsInfoList[m].tel);//联系电话
            el.setAttribute("driver",gpsInfoList[m].driver);//姓名
            el.setAttribute("status",gpsInfoList[m].status);//状态
            el.style.width = 70+"px";
            el.style.height = 28+"px";
            el.style.backgroundRepeat = "no-repeat";//设置背景图片不重复
            el.style.backgroundPosition = "center";//设置背景图片居中
            el.pitch = "map";//添加到map
            if(type == "1"){
                el.setAttribute("imgUrl",'url('+carOnImg+')');//在线图标
                el.style.backgroundImage = 'url('+carOnImg+')';

            }else {
                el.setAttribute("imgUrl",'url('+carOffImg+')');//离线图标
                el.style.backgroundImage = 'url('+carOffImg+')';//离线
            }
            let arr = [gpsInfoList[m].Longitude,gpsInfoList[m].Latitude];
            let lnglat = mapboxgl.LngLat.convert(arr);
            let carMarker = new mapboxgl.Marker(el)
                    .setLngLat(lnglat)
                let popupStr = `
`+gpsInfoList[m].VehicleId+`
车牌号:`+gpsInfoList[m].VehicleId+`
` let popup = new mapboxgl.Popup(
{offset:[0,-10],closeButton:false,closeOnClick:false}) .setHTML(popupStr) // .setText() //.setLngLat(lnglat) //.addTo(map) carMarker.setPopup(popup); carMarker.addTo(map); carMarker.push(carMarker); //给marker注册点击事件 el.onclick = function(){ if(popup){ popup.remove(); } this.carTel = this.getAttribute("tel");//电话 this.carDriver = this.getAttribute("driver");//司机姓名 let arr_ = [this.getAttribute("lon"),this.getAttribute("alt")]; let center_ = mapboxgl.LngLat.convert(arr_);//获取经纬度信息 let status = this.getAttribute("status"); let tmpCarId = this.getAttribute("id_"); if(status == "0"){ //离线 }else if(status == "1"){ //在线 }else{ } if(carMarker.length>0){ for(let i=0;i;i++){ if(tmpCarId !== carMarker[i].getElement().getAttribute("id_")){ if(carMarker[i].getPopup().isOpen()){ carMarker[i].togglePopup(); } } } } } }

你可能感兴趣的:(mapbox,js,vue)