腾讯地图vue组件,实现异步加载腾讯地图,坐标拾取器,支持按城市名称搜索。 搜索框样式依赖elementUI,不需要可删除顶部,地图部分无依赖项

//qqmap.vue
<template>
<div class="padd20">
<mapselect :mapcenter="centerLatLng" :oldmarker="oldMarker" @mapclick="pointChange"></mapselect>
</div> </template> <script type="text/ecmascript-6">
let qqMapSelectPoint = require('./selectPoint.vue');
export default{
components: {
mapselect: qqMapSelectPoint
},
data: function () {
return {
pointName: '郑州',
qqmap: null,
centerLatLng: '34.759666,113.648071',
oldMarker: '34.759666,113.648071',
newMarker: null
}
},
mounted(){ },
methods: {
pointChange(ev){
console.log('捕获到点击坐标', ev)
}
}
}
</script> <style>
.qqmap { width: 700px;
height: 600px;
}
</style>
//selectPoint.vue
<template>
<div>
<div style="overflow: hidden;">
<section>
<!--工具条-->
<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
<el-form :inline="true">
<el-form-item>
<el-input v-model="inputVal" placeholder="城市名称" @keyup.native="inputChange"
style="width: 300px;"></el-input>
</el-form-item>
<el-form-item>
<el-button type="primary" @click="search">获取地理位置</el-button>
</el-form-item>
<el-form-item>
<el-input v-model="latlngCurrent" placeholder="坐标值" style="width: 246px;"></el-input>
</el-form-item>
</el-form>
</el-col>
</section>
</div>
<div class="mapWrap">
<div class="qqmap" id="qqmapCont">
</div>
<div class="lngTips"></div>
</div> </div> </template> <script>
export default {
props: {
mapcenter: {
type: String,
default: '34.759666,113.648071'
},
oldmarker: {
type: String,
default: '34.759666,113.648071'
},
inputDefault: {
type: String,
default: ''
}
},
data: function () {
return {
qqmap: '',
premarker: '',
marker: '',
inputVal: '',
latlngCurrent: ''
}
},
mounted() {
let that = this;
this.inputVal = this.inputDefault;
if (this.oldmarker) {
this.latlngCurrent = this.oldmarker || this.mapcenter;
}
if (typeof(qq) == 'object') {
that.createMap();
} else {
this.loadQQmap();
}
window.onMapFileLoad = function () {
console.log('qqmap加载完成')
that.createMap();
}
},
watch: {
oldmarker(newVal, oldVal) {
if (newVal) {
console.log(typeof newVal)
this.latlngCurrent = newVal;
this.qqmap&&this.createMarker()
}
},
inputDefault(newVal, oldVal) {
console.log('默认地址变成:', newVal)
this.inputVal = newVal;
}
},
methods: {
loadQQmap() {
let script = document.createElement("script");
//设置标签的type属性
script.type = "text/javascript";
//设置标签的链接地址
script.src = "https://map.qq.com/api/js?v=2.exp&callback=onMapFileLoad";
//添加标签到dom
document.body.appendChild(script);
},
search() {
let that = this;
//调用地址解析类
let geocoder = new qq.maps.Geocoder({
complete: function (result) {
that.qqmap.setCenter(result.detail.location);
that.qqmap.setZoom(12)
}
});
let address = this.inputVal;
//通过getLocation();方法获取位置信息值
console.log('搜索地址:', address)
geocoder.getLocation(address);
},
inputChange() {
this.$emit('addr', this.inputVal); // 地图点击坐标 传递给父组件
},
createMap() {
let that = this;
this.qqmap = new qq.maps.Map(document.getElementById("qqmapCont"), {
center: new qq.maps.LatLng(that.mapcenter.split(',')[0], that.mapcenter.split(',')[1]), // 地图的中心地理坐标。
zoom: 12, // 地图的中心地理坐标。
});
setTimeout(() => {
this.createMarker();
this.bindMapEvent()
}, 500)
},
createMarker() {
let that = this;
if (that.premarker) {
that.premarker.setMap(null);
}
if (this.oldmarker) {
console.log('编辑模式:', this.oldmarker);
that.qqmap.setCenter(new qq.maps.LatLng(that.oldmarker.split(',')[0], that.oldmarker.split(',')[1]));
that.premarker = new qq.maps.Marker({
position: new qq.maps.LatLng(that.oldmarker.split(',')[0], that.oldmarker.split(',')[1]),
map: that.qqmap
});
} else {
//获取城市列表接口设置中心点
let citylocation = new qq.maps.CityService({
complete: function (result) {
that.qqmap.setCenter(result.detail.latLng);
}
});
//调用searchLocalCity();方法
citylocation.searchLocalCity();
}
},
bindMapEvent() {
let that = this;
// 地图点击事件
qq.maps.event.addListener(that.qqmap, 'click', function (event) {
that.marker && that.marker.setMap(null);
that.premarker && that.premarker.setMap(null);
that.$emit('mapclick', event.latLng); // 地图点击坐标 传递给父组件
that.latlngCurrent = event.latLng.lat + ',' + event.latLng.lng;
that.marker = new qq.maps.Marker({
position: event.latLng,
map: that.qqmap
});
});
// 地图鼠标滑动事件
let $lngTipsBox = document.querySelector('.lngTips');
let mapBoxWidth = window.getComputedStyle(document.querySelector('.mapWrap')).width;
qq.maps.event.addListener(that.qqmap, 'mousemove', function (event) {
$lngTipsBox.style.display = 'block';
$lngTipsBox.style.top = (event.pixel.y + 10) + 'px';
$lngTipsBox.style.left = (event.pixel.x + 15) + 'px';
$lngTipsBox.innerText = '点击选择此坐标:' + event.latLng.lat + ',' + event.latLng.lng;
});
// 鼠标离开
qq.maps.event.addListener(that.qqmap, 'mouseout', function (event) {
$lngTipsBox.style.display = 'none';
});
},
},
}
</script> <style>
.qqmap { width:100%; height:600px; }
.mapWrap { position:relative; width:100%; height:600px; overflow:hidden; margin-top:15px; }
.lngTips { display:none; width:255px; height:40px; padding:5px 7px; overflow:hidden; position:absolute; left:0; top:0; z-index:123456; background:#ffffff; border-radius:5px;
line-height:20px; box-shadow:#eeeeee 1px 1px 3px; border:#eeeeee 1px solid; font-size:12px; }
</style>

vue_qqmapdemo1的更多相关文章

随机推荐

  1. Chapter 2 栈和队列

    Chapter 2 栈和队列 1-   栈 当n个元素以某顺序进栈,可在任意时刻出栈,元素排列的顺序N满足Catalan()规则: 常用操作: 1   栈的初始化和定义: 2   元素x进栈: 3   ...

  2. JavaWeb-类加载器-注解-动态代理

    (一)类加载器 1.什么是类加载器,作用是什么? 类加载器就加载字节码文件(.class) 2.类加载器的种类 类加载器有三种,不同类加载器加载不同的 1)BootStrap:引导类加载器:加载都是最 ...

  3. Spring+quartz集群解决多服务器部署定时器重复执行的问题

    一.问题描述 Spring自带的Task虽然能很好使用定时任务,只需要做些简单的配置就可以了.不过如果部署在多台服务器上的时候,这样定时任务会在每台服务器都会执行,造成重复执行. 二.解决方案 Spr ...

  4. TZOJ 3522 Checker Challenge(深搜)

    描述 Examine the 6x6 checkerboard below and note that the six checkers are arranged on the board so th ...

  5. mysqldump与mydumper

    mydumper -u root -S /srv/my3308/run/mysql.sock -B trade_platform -o /data/trade_platform

  6. Frank Dellaert Slam Speech 20190708

    Georgia Institue of Tecknology 3D Models from Community Databases Spatiotemporal Reconstruction 4D C ...

  7. PHP学习1.5-预定义超全局数组变量

    1.PHP 预定义的超全局变量数组 特性: a.特殊的数组,操作方式没有区别 b.不用声明,php脚本中默认存在,因为在php中不用定义,所以在自定义变量是应避免和预定的全局变量同名 c.在全局范围内 ...

  8. 【python之路24】装饰器

    1.装饰器的应用场景 通常IT公司的程序开发是分工的,例如某公司某个部门负责底层函数的开发,另一个部门利用其函数实现高级功能,那么如果负责底层开发的函数需要改动,一般来说不会直接在函数上进行修改,通常 ...

  9. 20190807-RP-Explosion

    如题,RP爆发后爆炸了. 话说在七夕这天考试? 那么? 黑暗又来临了? 没有人见过那一幕. 在如漆般胶着的黑暗中, Struggle?Dying? 用鲜血浇灌花朵,用死亡迎接明天. 考试过程: 看看三 ...

  10. 使用 prerender 实现 SEO

    server { listen 80; server_name www.umount.com; access_log /var/log/nginx/livefrontend/access.log LF ...