123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251 |
- <template>
- <view class="uni-app">
- <view class="status-bar" />
- <view class="main-container">
- <wk-nav-bar title="选择位置" pervent @back="handleCancel">
- <!-- #ifndef MP-WEIXIN -->
- <!-- <button class="button white-btn" @click="handleConfirm">
- 确定
- </button> -->
- <!-- #endif -->
- </wk-nav-bar>
-
- <view class="container">
- <map
- id="chooseMap"
- :style="{
- width: mapStyle.width,
- flex: mapStyle.flex
- }"
- :longitude="centerPoint.longitude"
- :latitude="centerPoint.latitude"
- :markers="markers" />
-
- <view class="poi-container">
- <view
- v-for="(poi, index) in poiList"
- :key="poi.id"
- class="poi-item"
- @click="handleChoosePoi(index, $event)">
- <view class="left">
- <view class="poi-item-name">
- {{ poi.name }}
- </view>
- <view class="poi-item-address">
- {{ poi.address || '' }}
- </view>
- </view>
- <view class="right">
- <text v-if="activeIndex === index" class="wk wk-check icon" />
- </view>
- </view>
- </view>
- </view>
- </view>
- </view>
- </template>
- <script>
- import {
- getLocation,
- getNearbyPois
- } from '@/utils/map.js'
- import { deepCopy } from '@/utils/lib.js'
-
- export default {
- name: 'ChooseAddress',
- data() {
- return {
- centerPoint: {}, // 地图中心
-
- devicePoint: {}, // 设备地址
- poiList: [], // poi
- activeIndex: 0,
-
- mapStyle: {
- width: 0,
- flex: 1
- },
- markers: []
- }
- },
- mounted() {
- this.mapStyle = {
- width: uni.upx2px(750) + 'px',
- flex: 1
- }
- this.getLocation()
- },
- methods: {
- /**
- * 定位
- */
- getLocation() {
- getLocation(false).then(data => {
- console.log('定位结果:0.0', data)
- this.centerPoint = data
- this.devicePoint = deepCopy(this.centerPoint)
- this.getNearbyPois(data)
- }).catch(() => {
- this.$toast('定位失败')
- })
- },
-
- /**
- * 逆地址解析(获取附近的POI点)
- */
- getNearbyPois(data) {
- // >> 高德地图 <<
- // getNearbyPois({
- // location: `${data.longitude},${data.latitude}`,
- // extensions: 'all',
- // output: 'json'
- // }).then(data => {
- // console.log('位置信息: ', data)
- // this.centerPoint.address = data.regeocode.formatted_address
- // if (this.$isEmpty(this.centerPoint.address)) {
- // this.centerPoint.address = ''
- // }
- // this.devicePoint = deepCopy(this.centerPoint)
- // this.poiList = data.regeocode.pois.map(o => {
- // return {
- // ...o,
- // longitude: o.location.split(',')[0],
- // latitude: o.location.split(',')[1],
- // }
- // })
- // if (this.poiList.length > 0) {
- // this.poiList.unshift({
- // id: 1,
- // name: '我的位置',
- // address: this.centerPoint.address,
- // longitude: this.devicePoint.longitude,
- // latitude: this.devicePoint.latitude
- // })
- // this.handleChoosePoi(0)
- // } else {
- // this.markers = [this.centerPoint]
- // }
- // }).catch(() => {
- // this.$toast('获取位置信息失败')
- // })
-
- // >> 腾讯地图 <<
- getNearbyPois({
- location: `${data.latitude},${data.longitude}`,
- get_poi: 1,
- poi_options: 'radius=200'
- }).then(res => {
- console.log('位置信息: ', res)
- this.centerPoint.address = res.result.address
- if (this.$isEmpty(this.centerPoint.address)) {
- this.centerPoint.address = ''
- }
- this.devicePoint = deepCopy(this.centerPoint)
-
- this.poiList = res.result.pois.map(o => {
- return {
- ...o,
- name: o.title,
- longitude: o.location.lng,
- latitude: o.location.lat,
- }
- })
-
- if (this.poiList.length > 0) {
- this.poiList.unshift({
- id: 1,
- name: '我的位置',
- address: this.centerPoint.address,
- longitude: this.devicePoint.longitude,
- latitude: this.devicePoint.latitude
- })
- } else {
- this.markers = [this.centerPoint]
- }
- }).catch(() => {
- this.$toast('获取位置信息失败')
- })
- // -----------------------------------------------------------------
- },
-
- handleChoosePoi(index, evt) {
- this.activeIndex = index
- this.centerPoint = this.poiList[index]
- this.markers = [{
- ...this.centerPoint,
- width: 30,
- height: 30,
- iconPath: this.$static('images/map/location.png')
- }]
- if (evt) {
- this.handleConfirm()
- }
- },
-
- handleConfirm() {
- uni.$emit('choose-address', this.centerPoint)
- this.$Router.navigateBack()
- },
-
- handleCancel() {
- uni.$off('choose-address')
- this.$Router.navigateBack()
- }
- }
- }
- </script>
- <style scoped lang="scss">
- .container {
- flex: 1;
- width: 100%;
- display: flex;
- flex-direction: column;
- overflow: hidden;
- #chooseMap {
- width: 750rpx;
- }
-
- .poi-container {
- width: 100%;
- max-height: 450rpx;
- overflow: auto;
- background-color: white;
-
- .poi-item {
- padding: 15rpx 32rpx;
- border-bottom: 1rpx solid $border-color;
- @include left;
- .left {
- flex: 1;
- overflow: hidden;
- .poi-item-name {
- font-size: 30rpx;
- @include ellipsis;
- }
- .poi-item-address {
- font-size: 24rpx;
- color: $light;
- @include ellipsis;
- }
- }
- .right {
- width: 50rpx;
- margin-left: 20rpx;
- @include center;
- .icon {
- font-size: 36rpx;
- color: $theme-color;
- }
- }
- }
- .poi-item::last-child {
- border-bottom: 0 none;
- }
- }
- }
- </style>
|