在上一篇实现了侧边栏效果的基础上,引入腾讯地图,实现地图类型切换、2D、3D视图切换
效果如下:
一、使用腾讯位置服务
1.官网
腾讯位置服务 - 立足生态,连接未来
2.注册 登录
注册账号--->登录控制台
3.创建应用
应用管理--->我的应用--->创建应用--->填写应用名称,选择应用类型
例如:应用名称:maptest
应用类型:其他
4.添加key到「maptest」应用
填写Key名称--->描述--->勾选webServiceAPI--->添加
获取到应用的Key
二、在项目中引入地图
1.使用Visual Studio Code打开上篇中的项目demo,在public文件夹下,index.html添加script标签加载API服务,其中将YOUR_KEY替换为你自己创建的应用Key值。
2.在App.vue文件el-main中添加地图div容器,样式。
#mapcontainer{
width: 100%;
height: calc(100vh - 120px);
}
3.初始化地图
initmap(){
let center = new TMap.LatLng(24.874785,102.710112);
this.tMap = new TMap.Map('mapcontainer',{
zoom:10,
center:center,
})
},
完整代码:
// import HelloWorld from './components/HelloWorld.vue'
export default {
name: 'App',
components: {
// HelloWorld
},
data(){
return{
asideStatus:false,
aside_open_close:false,
tMap:null,//腾讯地图实例
}
},
mounted(){
// 初始化地图
this.initmap();
},
methods:{
// 侧边栏收缩与展开
asidechange(){
this.asideStatus = !this.asideStatus
if(this.asideStatus){
setTimeout(()=>{
this.aside_open_close =true
},500)
}else{
setTimeout(()=>{
this.aside_open_close =false
},500)
}
},
initmap(){
let center = new TMap.LatLng(24.874785,102.710112);
this.tMap = new TMap.Map('mapcontainer',{
zoom:10,
center:center,
})
},
}
}
.container{
height: 100%;
}
.el-header, .el-footer {
background-color: #B3C0D1;
color: #333;
text-align: center;
line-height: 60px;
}
/* .el-aside {
background-color: #D3DCE6;
color: #333;
text-align: center;
line-height: 200px;
} */
/* 侧边栏样式 */
.aside_main{
width: 200px !important;
transition: width 0.5s;
}
.aside_main_show{
width: 0px !important;
}
/* .el-main {
background-color: #E9EEF3;
color: #333;
text-align: center;
line-height: 160px;
} */
.main_cont{
position: relative;
margin: 0;
padding: 0;
background-color: #E9EEF3;
}
/* 侧边栏按钮样式 */
.aside_open_close{
position: absolute;
left: 0;
top: 50%;
width: 16px;
height: 60px;
line-height: 60px;
color: #fff;
background-color: #2A333A;
border-radius: 0 6px 6px 0;
font-size: 20px;
z-index: 1000;
cursor: pointer;
}
.aside_open_close:hover{
background-color: #FF8E2B;
color: #fff;
}
#mapcontainer{
width: 100%;
height: calc(100vh - 120px);
}
4.运行项目
此时发现出现错误提示!!!
在vue.config.js文件添加配置,重新运行即可
lintOnSave:false, //关闭eslint校验
三、实现地图类型切换、2D、3D视图切换
1.地图类型切换
.btncontainerLeft{
position: absolute;
left: 30px;
top: 10px;
z-index: 10000;
}
changebasemap(e){
if(e == '地图'){
this.tMap.setBaseMap({type:'vector'})
}else if(e == '卫星'){
this.tMap.setBaseMap({type:'satellite'})
}
},
2.地图视图切换
.btncontainerRight{
position: absolute;
right: 100px;
top: 10px;
z-index: 10000;
}
changeviewmode(el){
if(el == '2D'){
this.tMap.setViewMode('2D')
}else if(el == '3D'){
this.tMap.setViewMode('3D')
this.tMap.setPitch(70)
}
},
完整代码:
// import HelloWorld from './components/HelloWorld.vue'
export default {
name: 'App',
components: {
// HelloWorld
},
data(){
return{
asideStatus:false,
aside_open_close:false,
tMap:null,//腾讯地图实例
radio1:'地图',
radio2:'2D'
}
},
mounted(){
// 初始化地图
this.initmap();
},
methods:{
// 侧边栏收缩与展开
asidechange(){
this.asideStatus = !this.asideStatus
if(this.asideStatus){
setTimeout(()=>{
this.aside_open_close =true
},500)
}else{
setTimeout(()=>{
this.aside_open_close =false
},500)
}
},
initmap(){
let center = new TMap.LatLng(24.874785,102.710112);
this.tMap = new TMap.Map('mapcontainer',{
zoom:10,
center:center,
})
},
// 切换地图底图类型
changebasemap(e){
if(e == '地图'){
this.tMap.setBaseMap({type:'vector'})
}else if(e == '卫星'){
this.tMap.setBaseMap({type:'satellite'})
}
},
// 切换地图2D/3D视图
changeviewmode(el){
if(el == '2D'){
this.tMap.setViewMode('2D')
}else if(el == '3D'){
this.tMap.setViewMode('3D')
this.tMap.setPitch(70)
}
},
}
}
.container{
height: 100%;
}
.el-header, .el-footer {
background-color: #B3C0D1;
color: #333;
text-align: center;
line-height: 60px;
}
/* .el-aside {
background-color: #D3DCE6;
color: #333;
text-align: center;
line-height: 200px;
} */
/* 侧边栏样式 */
.aside_main{
width: 200px !important;
transition: width 0.5s;
}
.aside_main_show{
width: 0px !important;
}
/* .el-main {
background-color: #E9EEF3;
color: #333;
text-align: center;
line-height: 160px;
} */
.main_cont{
position: relative;
margin: 0;
padding: 0;
background-color: #E9EEF3;
}
/* 侧边栏按钮样式 */
.aside_open_close{
position: absolute;
left: 0;
top: 50%;
width: 16px;
height: 60px;
line-height: 60px;
color: #fff;
background-color: #2A333A;
border-radius: 0 6px 6px 0;
font-size: 20px;
z-index: 10000;
cursor: pointer;
}
.aside_open_close:hover{
background-color: #FF8E2B;
color: #fff;
}
#mapcontainer{
width: 100%;
height: calc(100vh - 120px);
}
.btncontainerLeft{
position: absolute;
left: 30px;
top: 10px;
z-index: 10000;
}
.btncontainerRight{
position: absolute;
right: 100px;
top: 10px;
z-index: 10000;
}
3.运行效果如下:
完结!!!
源码地址https://download.csdn.net/download/im_api/87495549
参考阅读
发表评论