Files
im/pages/common/map.vue
T

194 lines
4.2 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
<view style="display: flex; flex-direction: column; height: 100%;">
<!-- 使用web-view嵌入天地图 -->
<ly-map class="ly-map"
v-if="lng && lat"
@onUserEvent="onUserEvent"
ref="map"
2026-01-01 04:15:30 +08:00
:type="type"
2025-12-17 08:47:58 +08:00
: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'}">
2025-12-23 00:18:46 +08:00
<u-cell title="摇一摇" icon="/static/images/find/05.png" :size="cellSize"></u-cell>
<u-cell title="看一看" icon="/static/images/find/06.png" :size="cellSize"></u-cell>
<u-cell title="听一听" icon="/static/images/find/06.png" :size="cellSize"></u-cell>
<u-cell title="附近" icon="/static/images/find/08.png" :size="cellSize"></u-cell>
<u-cell title="购物" icon="/static/images/find/09.png" :size="cellSize"></u-cell>
2025-12-17 08:47:58 +08:00
</u-cell-group>
2025-12-11 22:33:31 +08:00
</view>
</view>
2026-01-01 04:15:30 +08:00
<view class="map_info" v-if="type == 'viewlocation'">
<view class="left">
<u-text wordWrap="anywhere" size="16" :text="address"></u-text>
</view>
<u-button @click="gotoMap" class="right">
<uni-icons size="36" color="#07c160" type="paperplane-filled"></uni-icons>
</u-button>
</view>
2025-12-11 22:33:31 +08:00
</view>
</template>
<script>
2026-01-01 04:15:30 +08:00
import util from "@/util/index.js"
2025-12-11 22:33:31 +08:00
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;
}
2026-01-01 04:15:30 +08:00
console.log(this.type)
2025-12-17 08:47:58 +08:00
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
}
},
2026-01-01 04:15:30 +08:00
gotoMap(){
util.toMapAPP(this.lng,this.lat,this.address);
},
2025-12-17 08:47:58 +08:00
onUserEvent(e) {
2026-01-01 04:15:30 +08:00
if(e.type == "move"){
if(this.type=='chooselocation'){
2025-12-17 08:47:58 +08:00
this.lng = e.lng;
this.lat = e.lat;
this.$refs.map.setMarkers([
{
lon: Number(e.lng),
lat: Number(e.lat)
}
]);
2026-01-01 04:15:30 +08:00
return ;
}
if(this.type=='viewlocation'){
return ;
2025-12-17 08:47:58 +08:00
}
2026-01-01 04:15:30 +08:00
return ;
}
if(e.type=='back'){
uni.navigateBack();
return ;
2025-12-17 08:47:58 +08:00
}
2026-01-01 04:15:30 +08:00
if(e.type=='confirm'){
this.confirm();
return ;
2025-12-11 22:33:31 +08:00
}
2026-01-01 04:15:30 +08:00
//console.log(e)
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%;
}
2026-01-01 04:15:30 +08:00
.map_info{
height: 100rpx;
padding: 50rpx 30rpx;
display: flex;
align-items: center;
justify-content: space-between;
.left{
flex:1;
}
.right{
width: 100rpx;
.u-button{
background-color: #ccc;
}
}
}
2025-12-11 22:33:31 +08:00
}
</style>