前端Vue基于騰訊地圖Api實現(xiàn)的選擇位置組件 返回地址名稱詳細地址經緯度信息胰舆,?下載完整代碼請訪問uni-app插件市場地址:https://ext.dcloud.net.cn/plugin?id=13310
效果圖如下:
#### 使用方法
```使用方法
<!-- leftTitle:左邊標題 name:輸入框名字 value:輸入框選擇值? placeholder:占位符 @click:點擊事件-->
<cc-locPicker leftTitle="收獲地點" name="location" :value="mapSelData.poiname" placeholder="請選擇位置"
@click="chooseAddress"></cc-locPicker>
<!-- 跳轉騰訊云地圖Api 頁面實現(xiàn) -->
<template>
<view class="map">
<!-- 騰訊地圖Api? key:騰訊地圖key -->
<web-view
src="https://apis.map.qq.com/tools/locpicker?search=1&type=1&key=SFABZ-WANWW-FISRY-3IGTF-HV7RE-YSFTI&referer=myapp"></web-view>
</view>
</template>
<script>
// 引入設置地址存儲工具
import {
setlocation
} from './utils.js'
// #ifdef H5
window.addEventListener('message', event => {
// 接收位置信息眼五,用戶選擇確認位置點后選點組件會觸發(fā)該事件藏否,回傳用戶的位置信息
var loc = event.data;
if (loc && loc.module == 'locationPicker') {
//防止其他應用也會向該頁面post信息坊秸,需判斷module是否為'locationPicker'
let location = {
poiaddress: loc.poiaddress,
poiname: loc.poiname,
latlng: loc.latlng
}
// 設置存儲地址信息
setlocation(location)
uni.navigateBack();
}
}, false);
// #endif
</script>
<style></style>
```
#### HTML代碼實現(xiàn)部分
```html
<template>
<view class="content">
<form @submit="formSubmit" @reset="formReset">
<!-- leftTitle:左邊標題 name:輸入框名字 value:輸入框選擇值? placeholder:占位符 @click:點擊事件-->
<cc-locPicker leftTitle="收獲地點" name="location" :value="mapSelData.poiname" placeholder="請選擇位置"
@click="chooseAddress"></cc-locPicker>
<ccInputView leftTitle="詳細地址" name="address" :value="mapSelData.poiaddress" placeholder="請輸入詳細地址">
</ccInputView>
<ccInputView leftTitle="經度信息" name="lng" :value="mapSelData.latlng.lng" placeholder="請輸入精度信息">
</ccInputView>
<ccInputView leftTitle="緯度信息" name="lat" :value="mapSelData.latlng.lat" placeholder="請輸入緯度信息">
</ccInputView>
<view class="uni-btn-v">
<button class="botBtn" type="primary" form-type="submit">下一步</button>
<view class="tipText"> 注意事項: 請確保您填寫的收獲位置準確 </view>
</view>
</form>
</view>
</template>
<script>
import ccInputView from '../../components/ccInputView.vue'
// 獲取地址工具
import {
getlocation
} from './utils.js'
export default {
components: {
ccInputView
},
data() {
return {
mapSelData: {
"latlng": {}
},
}
},
/**
* 生命周期函數(shù)--監(jiān)聽頁面顯示
*/
onShow: function() {
// #ifdef H5
let locations = getlocation() //獲取位置信息
if (locations) {
this.mapSelData = locations
uni.clearStorageSync();
}
// #endif
},
methods: {
formSubmit: function(e) {
console.log('form發(fā)生了submit事件旭贬,攜帶數(shù)據(jù)為:' + JSON.stringify(e));
var formdata = e.detail.value;
uni.showModal({
title: '溫馨提示',
content: 'formsubmit事件攜帶數(shù)據(jù)為:' + JSON.stringify(e.detail.value)
})
},
// 選擇地址
chooseAddress(e) {
let myThis = this;
uni.navigateTo({
url: './h5map'
})
},
}
}
</script>
<style>
.content {
display: flex;
flex-direction: column;
}
.uni-btn-v {
width: 100%;
height: auto;
}
.botBtn {
width: 90%;
margin-top: 36px;
}
.tipText {
width: 100%;
margin-left: 0px;
text-align: center;
color: #666666;
margin-top: 36px;
margin-bottom: 36px;
font-size: 28rpx;
}
</style>
```