vue项目中使用腾讯地图一些小记录

官网:JavaScript API | 腾讯位置服务

一、初始化地图

注册地图key,index.html中引入key

index.hrml

下面是vue页面实现内容

<div id="bgmap" style="width: 100%; height: 100vh;"></div>
data(){
            map: null,
            marker: null,
            infoWindow: null,//地图位点点击展示的弹窗
            // 当前展示的标记点数组
            markerArr: [
                {
                    add: "xxx"
                    address: "xxxx",
             
                    biz_id: "*****",
                    id: "********",
                    latitude: 22.82374749,//0-90
                    longitude: 113.26149523,//0-180
                    name: "testName",
                    park: 1,
                    styleId: "twotype",
                    
                },
                
            ],

             chooseItem: null,//选中的地图点信息
            showScaleVideo: false,//显示缩放
    
}

二、关键函数

init() ,初始化对象,调用地图marker函数绘制位点initMarkerObj(),调用位点展示函数showMarker()

 init() {
            var that = this
    
            this.map = new TMap.Map(document.getElementById("bgmap"), {
                center: new TMap.LatLng(22.820802, 113.261556),
                zoom: 11,
                mapStyleId: 'style2',//夜间模式
                viewMode: '3D',//3D模型
                baseMap: {
                    type: 'vector',
                    features: ['base', 'building3d'], // 隐藏矢量文字
                },
            });
            this.map.removeControl(TMap.constants.DEFAULT_CONTROL_ID.SCALE);//移除腾讯地图比例尺
            this.map.removeControl(TMap.constants.DEFAULT_CONTROL_ID.ROTATION);//移除腾讯地图旋转控件
            //移除腾讯地图缩放控件
            this.map.removeControl(TMap.constants.DEFAULT_CONTROL_ID.ZOOM);

            this.map.setZoom(17.3)//缩放比例
            this.map.setPitch(62);//3D倾斜角度
           
            this.initMarkerObj()
            this.showMarker()

            // setTimeout(function () {

            //     // 删除地图水印
            //     document.getElementById("bgmap").children[1].removeChild(document.getElementById("bgmap").children[1].children[1]);

            // }, 2000)
        },

2、地图点设置不同的样式 initMarkerObj()

根据官方提供案例,我设置不同的styles,使得位点可以使用不同的样式(onetype、twotype)

设置位点信息窗口TMap.InfoWindow,信息窗需要自己写入代码设置样式。

        initMarkerObj() {
            var that = this

            this.marker = new TMap.MultiMarker({
                id: 'marker_s',
                map: this.map, // 显示Marker图层的底图
                styles: {
                    onetype: new TMap.MarkerStyle({//onetype是呼吸点样式名称
                        // 点标注的相关样式
                        width: 78, // 宽度
                        height: 100, // 高度
                        anchor: { x: 39, y: 50 }, // 标注点图片的锚点位置
                        src: require("@/../static/icon/loca1.png"), // 标注点图片url或base64地址
                        color: '#fff', // 标注点文本颜色
                        size: 18, // 标注点文本文字大小
                        direction: 'top', // 标注点文本文字相对于标注点图片的方位
                        offset: { x: 0, y: 10 }, // 标注点文本文字基于direction方位的偏移属性
                        strokeColor: '#fff', // 标注点文本描边颜色
                        strokeWidth: 0.5, // 标注点文本描边宽度
                    }),
                    twotype: new TMap.MarkerStyle({
                        // 点标注的相关样式
                        width: 78, // 宽度
                        height: 100, // 高度
                        anchor: { x: 39, y: 50 }, // 标注点图片的锚点位置
                        src: require("@/../static/icon/car_loca1.png"), // 标注点图片url或base64地址
                        color: '#fff', // 标注点文本颜色
                        size: 18, // 标注点文本文字大小
                        direction: 'top', // 标注点文本文字相对于标注点图片的方位
                        offset: { x: 0, y: 10 }, // 标注点文本文字基于direction方位的偏移属性
                        strokeColor: '#fff', // 标注点文本描边颜色
                        strokeWidth: 0.5, // 标注点文本描边宽度
                    }),
                   
                },
                enableCollision: true, // 开启碰撞
                geometries: that.markerArr,//位点数组
            });

            // var infoWindow
            // 初始化infoWindow
            this.infoWindow = new TMap.InfoWindow({
                map: that.map,
                zIndex: 110,
                position: new TMap.LatLng(that.latitude, that.longitude),
                offset: { x: 0, y: -80 } //设置信息窗相对position偏移像素
            });
            this.infoWindow.close();//初始关闭信息窗关闭

            //监听回调函数(非匿名函数)
            var eventClick = function (e) {
                for (let i = 0; i < that.marker.geometries.length; i++) {
                    //判断是否点击某个位点 修改成点击状态
                    if (e.geometry.id === that.markerArr[i].id) {
                        that.chooseItem = that.markerArr[i]
                        if (that.markerArr[i].styleId == 'onetype' ) {
                            
                                that.markerArr[i].styleId = 'twotype'
                                that.markerArr[i].rank = 100
                            

                         that.infoWindow.open(); //打开信息窗
                         that.infoWindow.setPosition(e.geometry.position);//设置信息窗位置
                         that.infoWindow.setContent('<div id="infor_win"             
                              class="infor_win" style=" height:200px; width: 200px;">' +
                              '</div>');//设置信息窗内容        
                            })
                        }
                        else if (that.markerArr[i].styleId == 'twotype') {
                            
                                that.markerArr[i].styleId = 'onetype'
                                that.markerArr[i].rank = 1
                            
                            // that.marker.updateGeometries(that.markerArr)
                            that.infoWindow.close();
                        }

                    }
                    else {//其他未点击位点修改成未点击状态
                        that.markerArr[i].rank = 1
                        if (that.markerArr[i].styleId == 'twotype') {
                           
                                that.markerArr[i].styleId = 'onetype'
                            
                        }
                    }
                }

                that.marker.updateGeometries(that.markerArr)//更新位点样式
                that.$forceUpdate()
            }

            // 标记点点击事件
            this.marker.on("click", eventClick)

            that.$forceUpdate()
        },

3、自适应显示marker

        //设置自适应显示marker
        showMarker() {
            //初始化
            var bounds = new TMap.LatLngBounds();
            //判断标注点是否在范围内
            this.markerArr.forEach(function (item) {
                //若坐标点不在范围内,扩大bounds范围
                if (bounds.isEmpty() || !bounds.contains(item.position)) {
                    bounds.extend(item.position);
                }
            })
            //设置地图可视范围
            this.map.fitBounds(bounds, {
                padding: 100 // 自适应边距
            });
        },

4、效果

点击位点,切换样式效果。 

### Uniapp 中集成腾讯地图小程序 SDK 无响应解决方案 #### 检查环境配置 确保项目环境中已正确安装并配置了所需依赖项。对于 Vue Pinia 的初始化,在 `main.js` 文件中的设置如下所示[^3]: ```javascript import { createApp } from 'vue' import { createPinia } from 'pinia' import App from './App.vue' const pinia = createPinia() const app = createApp(App) app.use(pinia) app.mount('#app') ``` #### 确认 API 密钥有效性 验证使用腾讯地图 API Key 是否有效,并确认该密钥具有访问相应接口权限。API Key 可能因为过期或其他原因失效。 #### 安装与导入 SDK 按照官方文档指引完成腾讯地图 SDK 的安装过程,保证在 uni-app 项目里能够正常加载此插件库。通常情况下会涉及到 npm 或者 yarn 命令来获取最新版本的软件包。 #### 初始化 Map 实例 创建 map 组件实例时需注意参数传递准确性以及事件监听器绑定情况。下面是一个简单的例子说明如何在一个页面内启动地图功能: ```html <template> <view class="map-container"> <!-- 地图容器 --> <map id="myMap"></map> </view> </template> <script setup lang="ts"> // 引入必要的模块服务端口定义 import TMAP from '@/utils/tencent-map-sdk'; // 自行调整路径至实际位置 onMounted(() => { let myMap; try { // 创建地图对象 myMap = new TMAP.Map('myMap', { center: { lng: 116.404, lat: 39.915 }, // 设置中心点坐标 zoom: 12 // 初始缩放级别 }); console.log("成功初始化地图"); } catch (error) { console.error("地图初始化失败:", error); } }); </script> ``` #### 排除网络因素影响 考虑到可能存在由于网络状况不佳而导致的地图数据请求超时等问题,建议测试不同环境下应用的表现,比如本地 Wi-Fi 下对比移动数据连接下的表现差异。 #### 日志记录分析 利用浏览器开发者工具或者真机调试模式下查看控制台输出的日志信息,定位具体错误发生的位置及其上下文描述。这有助于进一步缩小问题范围直至找到根本原因。 #### 更新框架及插件 保持所使用的技术栈处于最新的稳定版状态,及时更新到更高版本往往可以修复一些潜在兼容性漏洞或是性能优化方面的问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值