在Vue项目中使用腾讯地图
实现结果
1、引入script
在public目录下,找到index.html文件,引入外部js文件,key为自己申请的腾讯地图地址
<script src="https://map.qq.com/api/gljs?v=1.exp&key=Y2ABZ-URA6Z-XDLXO-7AAIO-xxxxx-xxxxx&libraries=visualization"></script>
2、页面代码
2.1 这个div用来存放地图
<div ref="Container" style="width:1000px;height:600px;"/>
2.2 初始化地图
mounted() {
this.initMap()
},
initMap() {
//地图中心点坐标
var center = [39.909897147274364, 116.39756310116866]
// 定义map变量,调用 TMap.Map() 构造函数创建地图
var map= new window.TMap.Map(this.$refs.Container, {
center: new window.TMap.LatLng(center[0],center[1]), // 设置地图中心点坐标
zoom: 17.2, // 设置地图缩放级别
minZoom:8, //此处设置地图的最大最小缩放级别
maxZoom:17.2,
pitch:0, // 设置俯仰角
rotation: 45, // 设置地图旋转角度
showSatellite:true,
showControl:true,
})
2.3 绘制多边形
//地图上要显示的各个点坐标(拐点)
var pathStr = ['39.90189, 116.39156','39.90290, 116.39857','39.90392, 116.40360','39.90496, 116.39465']
var paths = []
for(var i=0; i<pathStr.length; i++){
paths.push(new window.TMap.LatLng(pathStr[i].split(",")[0], pathStr[i].split(",")[1]))
}
//绘制图层多边形(轮廓)
var polygon = new window.TMap.MultiPolygon({
id: 'polygon-layer', // 图层id
map:map, // 显示多边形图层的底图
styles: {
// 多边形的相关样式
polygon: new window.TMap.PolygonStyle({
color: 'rgba(0,125,255,0.4)', // 面填充色
showBorder: true, // 是否显示拔起面的边线
borderColor: '#00FFFF', // 边线颜色
}),
},
geometries: [
{
id: 'polygon', // 多边形图形数据的标志信息
styleId: 'polygon', // 样式id
paths: paths, // 多边形的位置信息
properties: {
// 多边形的属性数据
title: 'polygon',
},
},
],
});
2.4 添加标志性建筑
var geometries = []
paths.forEach(item => {
geometries.push({
"id": item[0],
"styleId": item[0],
"position": item,
"properties": {
"title": item[0]
}
})
})
var marker = new window.TMap.MultiMarker({
//指定容器为上面的地图
map: map,
styles: {
// 点标记样式
marker: new window.TMap.MarkerStyle({
width: 20, // 样式宽
height: 30, // 样式高
url: '../assets/icon/map-marker.png', //图片路径
anchor: { x: 10, y: 30 }, // 描点位置
}),
},
//点标记数据数据
geometries: geometries
});
2.5 鼠标移动到建筑上显示 提示内容
var infoWindow = new window.TMap.InfoWindow({
//同上,容器
map: map,
position: new window.TMap.LatLng(center[0], center[1]),
offset: { x: 0, y: -32 } //设置信息窗相对position偏移像素
});
infoWindow.close();//初始关闭信息窗关闭
marker.on("mouseover", function (evt) {
//设置infoWindow
infoWindow.open(); //打开信息窗
infoWindow.setPosition(evt.geometry.position);//设置信息窗位置
infoWindow.setContent(evt.geometry.position.toString());//设置信息窗内容
})
2.6 绘制热力图
let heatData = [{"lng":116.39857,"lat":39.90290,"count":100},{"lng":116.39156,"lat":39.90189,"count":211}] //热力数据
let heatMap = new window.TMap.visualization.Heat({
max: 250, // 热力最强阈值
min: 0, // 热力最弱阈值
height: 0, // 峰值高度
gradientColor: { // 渐变颜色
0.6: "#673198",
0.8: "#e53390",
0.9: "#ffc95a",
},
radius: 30 // 最大辐射半径
}).addTo(map);
heatMap.setData(heatData);//设置数据
3 完整代码
<template>
<div ref="Container" style="width:1000px;height:600px;"/>
</template>
<script>
export default {
data() {
return {
}
},
mounted() {
this.initMap()
},
methods: {
// 地图初始化函数
initMap() {
//地图中心点坐标
var center = [39.909897147274364, 116.39756310116866]
// 定义map变量,调用 TMap.Map() 构造函数创建地图
var map= new window.TMap.Map(this.$refs.Container, {
center: new window.TMap.LatLng(center[0],center[1]), // 设置地图中心点坐标
zoom: 17.2, // 设置地图缩放级别
minZoom:8, //此处设置地图的最大最小缩放级别
maxZoom:17.2,
pitch:0, // 设置俯仰角
rotation: 45, // 设置地图旋转角度
showSatellite:true,
showControl:true,
})
//地图上要显示的各个点坐标(拐点)
var pathStr = ['39.90189, 116.39156','39.90290, 116.39857','39.90392, 116.40360','39.90496, 116.39465']
var paths = []
for(var i=0; i<pathStr.length; i++){
paths.push(new window.TMap.LatLng(pathStr[i].split(",")[0], pathStr[i].split(",")[1]))
}
//绘制图层多边形(轮廓)
var polygon = new window.TMap.MultiPolygon({
id: 'polygon-layer', // 图层id
map:map, // 显示多边形图层的底图
styles: {
// 多边形的相关样式
polygon: new window.TMap.PolygonStyle({
color: 'rgba(0,125,255,0.4)', // 面填充色
showBorder: true, // 是否显示拔起面的边线
borderColor: '#00FFFF', // 边线颜色
}),
},
geometries: [
{
id: 'polygon', // 多边形图形数据的标志信息
styleId: 'polygon', // 样式id
paths: paths, // 多边形的位置信息
properties: {
// 多边形的属性数据
title: 'polygon',
},
},
],
});
//给地图添加点坐标 (标志性建筑,这里坐标用的上面的拐点)
var geometries = []
paths.forEach(item => {
geometries.push({
"id": item[0],
"styleId": item[0],
"position": item,
"properties": {
"title": item[0]
}
})
})
var marker = new window.TMap.MultiMarker({
//指定容器为上面的地图
map: map,
styles: {
// 点标记样式
marker: new window.TMap.MarkerStyle({
width: 20, // 样式宽
height: 30, // 样式高
url: '../assets/icon/map-marker.png', //图片路径
anchor: { x: 10, y: 30 }, // 描点位置
}),
},
//点标记数据数据
geometries: geometries
});
//点击标记点弹出的数据
var infoWindow = new window.TMap.InfoWindow({
//同上,容器
map: map,
position: new window.TMap.LatLng(center[0], center[1]),
offset: { x: 0, y: -32 } //设置信息窗相对position偏移像素
});
infoWindow.close();//初始关闭信息窗关闭
marker.on("mouseover", function (evt) {
//设置infoWindow
infoWindow.open(); //打开信息窗
infoWindow.setPosition(evt.geometry.position);//设置信息窗位置
infoWindow.setContent(evt.geometry.position.toString());//设置信息窗内容
})
//绘制热力图
let heatData = [{"lng":116.39857,"lat":39.90290,"count":100},{"lng":116.39156,"lat":39.90189,"count":211}] //热力数据
let heatMap = new window.TMap.visualization.Heat({
max: 250, // 热力最强阈值
min: 0, // 热力最弱阈值
height: 0, // 峰值高度
gradientColor: { // 渐变颜色
0.6: "#673198",
0.8: "#e53390",
0.9: "#ffc95a",
},
radius: 30 // 最大辐射半径
}).addTo(map);
heatMap.setData(heatData);//设置数据
},
}
}
</script>
新手小白,欢迎指正!