Files
im/pages/common/map.vue
T

160 lines
3.7 KiB
Vue
Raw Normal View History

2025-12-11 22:33:31 +08:00
<template>
<view class="map_page">
2025-12-17 08:47:58 +08:00
<u-navbar left-icon="arrow-left" @leftClick="back" placeholder bgColor="transparent">
2025-12-11 22:33:31 +08:00
<template slot="right">
2025-12-17 08:47:58 +08:00
<u-button type="primary" class="confirm_btn" size="mini" @click="confirm">确定</u-button>
2025-12-11 22:33:31 +08:00
</template>
2025-12-17 08:47:58 +08:00
</u-navbar>
<view style="display: flex; flex-direction: column; height: 100%;">
<!-- 使用web-view嵌入天地图 -->
<ly-map class="ly-map"
v-if="lng && lat"
@onUserEvent="onUserEvent"
ref="map"
:lonlat=[lng,lat]
:map-key="apikey" />
<view class="search_container" v-if="1==2">
<u-search placeholder="日照香炉生紫烟" v-model="keyword"></u-search>
<u-cell-group :customStyle="{backgroundColor:'#FFF'}">
<u-cell title="摇一摇" icon="/static/images/workbench/05.png" :size="cellSize"></u-cell>
<u-cell title="看一看" icon="/static/images/workbench/06.png" :size="cellSize"></u-cell>
<u-cell title="听一听" icon="/static/images/workbench/06.png" :size="cellSize"></u-cell>
<u-cell title="附近" icon="/static/images/workbench/08.png" :size="cellSize"></u-cell>
<u-cell title="购物" icon="/static/images/workbench/09.png" :size="cellSize"></u-cell>
</u-cell-group>
2025-12-11 22:33:31 +08:00
</view>
</view>
</view>
</template>
<script>
export default {
data() {
return {
2025-12-17 08:47:58 +08:00
keyword:"",
cellSize:"base",
apikey:"ecc44b16c51c888d625f0238d678a61b",
type:"chooselocation",
lng:"",
lat:"",
address:"",
2025-12-11 22:33:31 +08:00
}
},
2025-12-17 08:47:58 +08:00
async onLoad(opt) {
if(opt.type){
this.type = opt.type;
}
if(opt.lng){
this.lng = opt.lng;
2025-12-11 22:33:31 +08:00
}
2025-12-17 08:47:58 +08:00
if(opt.lat){
this.lat = opt.lat;
}
if(opt.address){
this.address = opt.address;
}
this.init();
2025-12-11 22:33:31 +08:00
},
methods: {
2025-12-17 08:47:58 +08:00
// 初始化
init() {
const _this = this;
if(this.type=='chooselocation'){
uni.getLocation({
success(res) {
_this.lng = res.longitude;
_this.lat = res.latitude;
2025-12-11 22:33:31 +08:00
},
2025-12-17 08:47:58 +08:00
fail() {
_this.lng = 113;
_this.lat = 40;
2025-12-11 22:33:31 +08:00
}
2025-12-17 08:47:58 +08:00
})
2025-12-11 22:33:31 +08:00
}
},
2025-12-17 08:47:58 +08:00
onUserEvent(e) {
//console.log(e)
if(this.type=='chooselocation'){
if(e.type == "move"){
this.lng = e.lng;
this.lat = e.lat;
this.$refs.map.setMarkers([
{
lon: Number(e.lng),
lat: Number(e.lat)
}
]);
}
}
if(this.type=='viewlocation'){
2025-12-11 22:33:31 +08:00
}
},
/**
* 确定位置按钮点击
*/
confirm() {
const _this = this;
2025-12-17 08:47:58 +08:00
if (!this.lng || !this.lat) {
2025-12-11 22:33:31 +08:00
uni.showToast({
2025-12-17 08:47:58 +08:00
title: '请选择位置',
2025-12-11 22:33:31 +08:00
icon: 'none'
});
return;
}
2025-12-17 08:47:58 +08:00
const url=`http://api.tianditu.gov.cn/geocoder?postStr={"lon":${this.lng},"lat":${this.lat},"ver":1}&type=geocode&tk=${this.apikey}`;
console.log(url);
uni.request({
url:url,
success(res){
//console.log(res.data);
const result = res.data.result;
_this.address = result.formatted_address;
console.log( {
lng: result.location.lon,
lat: result.location.lat,
address: result.formatted_address
});
//return 1;
// 通过事件通道返回数据给父页面
const eventChannel = _this.getOpenerEventChannel();
eventChannel.emit('onConfirm', {
lng: result.location.lon,
lat: result.location.lat,
address: result.formatted_address
});
uni.navigateBack();
},
2025-12-11 22:33:31 +08:00
});
2025-12-17 08:47:58 +08:00
return 1;
2025-12-11 22:33:31 +08:00
},
/**
* 返回上一页
*/
back() {
uni.navigateBack();
}
}
}
</script>
<style lang="scss" scoped>
.map_page {
display: flex;
flex-direction: column;
height: 100vh;
width: 100vw;
background: #fff;
position: relative;
overflow: hidden;
2025-12-17 08:47:58 +08:00
.map_container {
2025-12-11 22:33:31 +08:00
flex: 1;
width: 100%;
}
}
2025-12-17 08:47:58 +08:00
.confirm_btn{
padding: 30rpx 10rpx;
2025-12-11 22:33:31 +08:00
}
</style>