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

25 篇文章 0 订阅
6 篇文章 1 订阅
/**
         * 创建自定义marker图标绑定弹框
        */
       let carMarker = [];//存放marker图标
       for(let m= 0;m<gpsInfoList.length;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 = `<div class = "popupCar" style = "font-size:12px;width:160px;padding:5px 8px 10 px 8px;box-sizing:border-box;background-color:rgba(56,58,69,0.9);box-shadow:3px 3px 5px rgba(0,0,0,0.6);color:#fff;border-radius:8px;">
                                <div style="height:32px;line-height:32px;font-size:14px;color:#18dd91;border-bottom:1px solid:#b0bcdb;margin-bottom:3px;">`+gpsInfoList[m].VehicleId+`</div>
                                <div style="height:32px;line-height:32px;font-size:14px;color:#18dd91;"><span>车牌号:</span><span>`+gpsInfoList[m].VehicleId+`</span></div>

                    
                    </div>`
                    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<carMarker.length;i++){
                                if(tmpCarId !== carMarker[i].getElement().getAttribute("id_")){
                                    if(carMarker[i].getPopup().isOpen()){
                                        carMarker[i].togglePopup();
                                    }
                                }
                            }
                        }
                    }
        }
自定义 Mapbox 的散点图标,需要按照以下步骤: 1.准备图标文件 首先,需要准备散点图标的图像文件。可以使用任何常见的图像格式,例如 PNG、SVG、JPG 等。确保图像文件不透明,并且大小不超过 512 x 512 像素。 2.将图标上传到 Mapbox 登录 Mapbox 网站,进入“样式”页面。然后,选择要使用的样式并进入编辑模式。在左侧菜单中,选择“图标”选项卡,然后单击“添加图标”按钮。接着,选择要上传的图像文件并指定图标 ID。注意,图标 ID 必须是唯一的,并且只能包含字母、数字和下划线。 3.在地图上使用自定义图标 现在,可以在地图上使用自定义图标。可以使用 Mapbox GL JS 或 Mapbox Android SDK 等 Mapbox SDK 来实现。 在 Mapbox GL JS 中,可以使用以下代码来设置散点图标的样式: ``` map.addLayer({ id: 'points', type: 'symbol', source: { type: 'geojson', data: { type: 'FeatureCollection', features: [{ type: 'Feature', geometry: { type: 'Point', coordinates: [longitude, latitude] } }] } }, layout: { 'icon-image': 'icon-id', 'icon-size': 0.5 } }); ``` 其中,'icon-image' 属性指定要使用的图标 ID,'icon-size' 属性指定图标大小。 在 Mapbox Android SDK 中,可以使用以下代码来设置散点图标的样式: ``` mapboxMap.addMarker(new MarkerOptions() .position(new LatLng(latitude, longitude)) .icon(IconFactory.getInstance(context).fromResource(R.drawable.icon_id)) .title("Marker Title")); ``` 其中,'R.drawable.icon_id' 指定要使用的图标资源 ID。 以上是自定义 Mapbox 散点图标的基本步骤。如果需要更高级的样式控制,可以参考 Mapbox 官方文档或搜索相关教程。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值