前言
微信小程序項目需要實現輸入地址搜索解析出相應經緯度並在地圖上打點標注。
前期准備
1、 申請騰訊位置服務key
2、npm install qqmap --save
引入需要的js文件
在App.vue中輸入
<script type="text/javascript" src="http://map.qq.com/api/js?v=2.exp&key=申請的key"></script>
<script type="text/javascript" src="https://3gimg.qq.com/lightmap/components/geolocation/geolocation.min.js"></script>
新建TMap.js文件
import maps from 'qqmap';
export function TMap() {
return new Promise(resolve => {
maps.init(申請的key, () => {
resolve(maps);
});
});
}
新建map.vue文件
<template>
<div id="container"></div>
</template>
<script>
/* eslint-disable */
import { TMap } from "./TMap";
export default {
name: "mapChild",
data() {
return {};
},
created() {
let _this = this;
TMap().then(qq => {
//初始化中心點,傳入想要設置的值
this.mapInit(經度, 緯度, 縮放比例);
});
},
methods: {
//父組件調用該函數,設置地點
setLoc(lng, lat) {
this.mapInit(lng, lat, 16);
},
//搜索某一地點名
getLoc(ele) {
this.$axios({
url: url,
//直接使用騰訊的搜索api的話會報跨域錯誤
//我是通過node服務端作為代理去請求數據
//所以這里就不放出實際ip地址了哈
//在最后我會將node部分的代碼也加上
method: "get",
params: {
address: ele
}
})
.then(res => {
let searchObj = res.data.data;
searchObj.search = 1;
this.$emit("mapSend", searchObj);
let _this = this;
let resultData = res.data.data.data[0];
if (res.data.data.status == 0) {
this.mapInit(resultData.location.lng, resultData.location.lat, 16);
}
})
.catch(error => {
console.log(error);
});
},
//根據傳入的值渲染地圖及傳出經緯度和地名
mapInit(lng,lat,zoom) {
let _this = this
var map = new qq.maps.Map(document.getElementById("container"), {
// 地圖的中心地理坐標。
center: new qq.maps.LatLng(
lat,
lng
),
zoom: zoom
});
var marker = new qq.maps.Marker({
position: new qq.maps.LatLng(
lat,
lng
),
map: map
});
qq.maps.event.addListener(map, "click", function(event) {
marker.setMap(null);
});
qq.maps.event.addListener(map, "click", function(event) {
let result = {
status: 0,
result: {
location: {
lng: event.latLng.getLng(),
lat: event.latLng.getLat()
}
}
};
qq.maps.event.addListener(map, "click", function(event) {
marker.setMap(null);
});
var marker = new qq.maps.Marker({
position: event.latLng,
map: map
});
_this
.$axios({
url: url,
//這里的url跟上面也是相同的問題
method: "get",
params: {
location: event.latLng.getLat() + "," + event.latLng.getLng()
}
})
.then(res => {
res.data.data.extra = 1;
_this.$emit("mapSend", res.data.data);
})
.catch(err => {
this.$message({
type: 'warning',
message: '定位解析失敗'
})
})
});
}
},
};
</script>
<style>
#container {
min-width: 600px;
min-height: 400px;
}
</style>
以上就完成了子組件的創建,然后就可以在父組件中引入並使用
效果圖
node部分代碼
//獲取地點
router.get('/tmapA', async function (req, res, next) {
let url = 'http://apis.map.qq.com/ws/place/v1/suggestion/?key=申請的key®ion=' + urlencode('紹興','utf-8') + '&keyword=' + urlencode(req.query.address,'utf-8')
request({
url: url,
method: "GET",
json: true,
}, function(_err, _res, _resBody){
if(_resBody){
new Result(_resBody, '解析成功').success(res)
}else{
new Result(null, '解析失敗').fail(res)
}
})
})
//獲取經緯度
router.get('/tmapL', async function (req, res, next) {
let url = 'https://apis.map.qq.com/ws/geocoder/v1/?key=申請的key&location=' + req.query.location
request({
url: url,
method: "GET",
json: true,
}, function(_err, _res, _resBody){
if(_resBody){
new Result(_resBody, '解析成功').success(res)
}else{
new Result(null, '解析失敗').fail(res)
}
})
})
作者:yiyou12138
鏈接:https://segmentfault.com/a/1190000022763174
來源:SegmentFault
著作權歸作者所有。商業轉載請聯系作者獲得授權,非商業轉載請注明出處。