騰訊地圖+element-ui 實現地址搜尋標記功能
阿新 • • 發佈:2020-12-28
前言
微信小程式專案需要實現輸入地址搜尋解析出相應經緯度並在地圖上打點標註。
前期準備
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
著作權歸作者所有。商業轉載請聯絡作者獲得授權,非商業轉載請註明出處。