123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306 |
- <template>
- <view class="server-place">
- <map id='map' ref='map' v-bind:style="{height: mapH + 'px'}" style="width: 100%;" :latitude="latitude" :longitude="longitude"
- :controls='controls' scale="18" @regionchange='mapChange'>
- </map>
- <view class="map-tools">
- <view class="my-location">
- <image class="left" :src="myPositionIcon" mode="" @tap="toMyLocation"></image>
- <view class="right">
- <text class="title">我的位置</text>
- <text class="text">{{myAddress.addressInfo}}</text>
- </view>
- </view>
- <view class="start-place">
- <view class="place">
- <text class="text">{{addressObj.address.addressInfo}}</text>
- </view>
- <view class="tip">{{descText}}</view>
- </view>
- <button @tap="submitAdress" class="sure" type="primary">确认选择</button>
- </view>
- </view>
- </template>
- <script>
- const app = getApp()
- var QQMapWX = require('./qqmap-wx-jssdk.min.js')
- var qqmapsdk = new QQMapWX({
- key: 'LXCBZ-NNIKD-UZ64F-H6AFI-UNJLH-OCFGE'
- })
- export default {
- props: {
- tipText: {
- type: String,
- default: '选择位置'
- },
- descText: {
- type: String,
- default: '使用当前定位或在地图上标记位置'
- },
- positionIcon: {
- type: String,
- default: '../../static/sailun/gps-icon.png'
- },
- myPositionIcon: {
- type: String,
- default: '../../static/sailun/8CjxSJ.png'
- }
- },
- data() {
- return {
- mapH: 0, // 地图高度,可在initMapH()中设置高度
- longitude: 0, // 初始经度
- latitude: 0, // 初始纬度
- myAddress: '', // 初始地址信息
- addressObj: { // 地图选点信息
- longitude: '',
- latitude: '',
- address: '请选择集合地点'
- },
- controls: [], // 地图中心点图标, 可更换iconPath, 详情见官方文档关于map组件的介绍
- };
- },
- mounted() {
- this.getLocation()
- this.initMapH()
- this.initPositionIcon()
- },
- methods: {
- // 初始化地图中心位置的定位图片
- initPositionIcon() {
- setTimeout(() => {
- this.controls = [{
- iconPath: this.positionIcon,
- position: {
- left: uni.getSystemInfoSync().windowWidth/2-15,
- top: (uni.getSystemInfoSync().windowHeight-240)/2-15,
- width: 30,
- height: 30,
- },
- clickable: false
- }]
- }, 100)
- },
- // 查询现在的位置
- getLocation() {
- let this_ = this
- uni.getLocation({
- type: 'gcj02', // 返回国测局坐标
- geocode: true,
- success: function(res) {
- this_.initMap(res)
- },
- fail: function(e) {
- uni.showToast({
- icon: 'none',
- title: '获取地址失败, 请检查是否开启定位权限'
- })
- }
- })
- },
- // 初始化我的位置
- async initMap(res) {
- this.longitude = res.longitude;
- this.latitude = res.latitude;
- this.myAddress = await this.getAddressName(res);
- this.addressObj = Object.assign({}, this.addressObj, {
- longitude: res.longitude,
- latitude: res.latitude,
- address: this.myAddress
- })
- },
- // 地图选择位置后 查询地点名称
- async checkMap(res) {
- this.addressObj = Object.assign({}, this.addressObj, {
- longitude: res.longitude,
- latitude: res.latitude,
- address: await this.getAddressName(res)
- })
- },
- // 监听地图位置变化
- mapChange(e) {
- let that = this
- clearTimeout(this.timer)
- this.timer = setTimeout(() => {
- if (e.type == 'regionchange' || e.type == 'end') {
- that.mapCtx = uni.createMapContext('map', this)
- that.mapCtx.getCenterLocation({
- success: res => {
- this.checkMap(res)
- },
- fail: err => {
-
- }
- })
- }
- }, 200)
- },
- // 查询地图中心点的名称
- getAddressName(addressObj) {
- return new Promise((res) => {
- // #ifdef APP-PLUS
- qqmapsdk.reverseGeocoder({
- location: {
- latitude: addressObj.latitude,
- longitude: addressObj.longitude
- },
- get_poi: 1,
- poi_options: "page_size=1;page_index=1",
- output: 'jsonp',
- success: (e) => {
- res({addressInfo:e.result.formatted_addresses.recommend,province:e.result.address_component.province,city:e.result.address_component.city,district:e.result.address_component.district,});
- },
- fail: err => {
- res(err);
- }
- })
- // #endif
- // #ifndef APP-PLUS
- // ======================== jsonp跨域 ========================
- const KEY = 'LXCBZ-NNIKD-UZ64F-H6AFI-UNJLH-OCFGE'
- let locationObj = addressObj.latitude + ',' + addressObj.longitude
- let url =
- 'https://apis.map.qq.com/ws/geocoder/v1?coord_type=5&get_poi=1&output=jsonp&poi_options=page_size=1;page_index=1';
- this.$jsonp(url, {
- key: KEY,
- location: locationObj
- }).then(e => {
- res(e.result.formatted_addresses.recommend);
- })
- .catch(err => {
- res(err);
- })
- // #endif
- })
- },
- // 计算地图的高度
- initMapH() {
- this.mapH = uni.getSystemInfoSync().windowHeight-210;
- },
- // 移动到我的位置
- toMyLocation() {
- this.getLocation()
- },
- // 提交
- submitAdress() {
- if(this.addressObj.address.message=="request:fail abort statusCode:-1"){
- this.$u.toast('正在解析地址,请稍候');
- return
- }
- // console.log(this.addressObj)
- this.controls = []
- setTimeout(() => {
- this.$emit('updateAddress', this.addressObj)
- }, 100)
- }
- },
- }
- </script>
- <style lang="scss" scoped>
- .server-place {
- height: 100vh;
- width: 100%;
- z-index: 999;
- .map-tools {
- width: 750rpx;
- display: flex;
- justify-content: center;
- align-items: center;
- flex-direction: column;
- .my-location {
- width: 700rpx;
- height: 100rpx;
- box-shadow: 0rpx 3rpx 20rpx rgba(0, 0, 0, 0.2);
- background: #fff;
- border-radius: 10rpx;
- display: flex;
- justify-content: flex-start;
- align-items: center;
- overflow: hidden;
- margin-top: 10rpx;
- .left {
- background: #3384ff;
- // flex: 20%;
- width: 100rpx;
- height: 100%;
- }
- .right {
- font-size: 26rpx;
- margin-left: 10rpx;
- color: #111;
- // flex: 80%;
- display: flex;
- justify-content: center;
- align-items: flex-start;
- flex-direction: column;
- .text {
- width: 500rpx;
- overflow: hidden;
- white-space: nowrap;
- text-overflow: ellipsis;
- color: #3384FF;
- margin-top: 10rpx;
- }
- }
- }
- .start-place {
- width: 700rpx;
- height: 100rpx;
- box-shadow: 0rpx 3rpx 20rpx rgba(0, 0, 0, 0.2);
- background: #fff;
- border-radius: 10rpx;
- margin-top: 20rpx;
- .place {
- margin-left: 20rpx;
- .title {
- font-size: 24rpx;
- font-weight: bold;
- color: #111;
- }
- .text {
- margin-top: 20rpx;
- font-size: 24rpx;
- color: #3384FF;
- font-weight: bold;
- width: 700rpx;
- vertical-align: middle;
- display: inline-block;
- overflow: hidden;
- white-space: nowrap;
- text-overflow: ellipsis;
- }
- }
- .tip {
- margin-left: 20rpx;
- font-size:20rpx;
- color: #666;
- }
- }
- .sure {
- font-weight: 600;
- width: 700rpx;
- margin-top: 20rpx;
- }
- }
- }
- </style>
|