uni-app 微信小程序中关于 map 地图使用案例分享

 这篇文章,我将带领大家使用微信内置地图详细讲解关于如何配置地图打开地图使用地图

目录

1、配置地图

2、先写页面内,这里我创建了一个 map示例页面 mapDemo.vue

2.1 关于 map 组件各个属性的说明可以详见官方文档。

 2.2 在使用 uni.getSetting() 方法来获取用户的当前设置,每次进入程序判断当前是否获得授权,如果没有,就去调起弹窗请求获得授权,如果获得授权,就直接获取当前地理位置。

3、实现调用地图组件方式一

3.1 首先在 onLoad 方法内触发 isGetLocation()方法

3.2 点击右上角确定按钮时,getLocationInfo() 方法会返回具体位置信息。

 4、实现调用地图组件方式二

4.1 先去获取当前所在位置的经纬度

4.2 很据纬度获取详细的地址 

4.3 再把控件定在地图的中心点,然后手动拖动地图,从而实现手动选择收货地址的功能。

 5、完整代码示例 mapDemo.vue 内容如下:

6、结尾


uni-app 官方文档如下:

map | uni-app官网

 微信小程序官方文档如下:

wx.openLocation(Object object) | 微信开放文档

 个人建议,如果是 uni-app 开发,那就直接按照 uni-app 的文档来做。如果有差异的你可以再去看看微信的文档,因为本身 uni-app 就已经帮我们做好了集成封装。

1、配置地图

打开manifest.json 勾选位置接口,并添加描叙信息:你的位置信息将用于小程序位置接口的效果展示

 这里点击保存,切到源码视图就会发现在 mp-weixin 下多了如下内容:

"permission" : {
            "scope.userLocation" : {
                "desc" : "你的位置信息将用于小程序位置接口的效果展示"
            }
        },

2、先写页面内,这里我创建了一个 map示例页面 mapDemo.vue

<template>
    <view class="map_wrap">
        <map id="mapSelected" style="width: 690rpx; height: 300px; margin-left: 30rpx;" :latitude="latitude" :longitude="longitude" :markers="covers" :controls="controls" :scale="18" @controltap="controltap" @tap="clickMap" 
        @regionchange="regionchange">
        </map>
        
		<view class="mform">
			<view class="item">
				<view class="t">当前地址:</view>
				<view class="m">{{address}}</view>
			</view>
			<view class="item">
				<view class="t">推荐地址:</view>
				<view class="m">{{address_info}}</view>
			</view>
		</view>
        
        <!-- 保存按钮这里具体保存事件我没写,根据实际需要调用后台保存地址即可 -->
		<view class="btnbox">
			<button class="btn" @click="saveAddress()" style="width:85%;">保存</button>
		</view>
    </view>
</template>

页面数据项 data 部分

data() {
		return {
			title: 'map',
			latitude: 39.909,// 默认纬度
			longitude: 116.39742,// 默认经度(北京天安门)
			covers: [{
				id: 110, 
				latitude: 39.909,
				longitude: 116.39742,
				iconPath: '	https://hellouniapp.dcloud.net.cn/static/location.png',
				width: "20",
				height: "20"
			}],
			controls: [{ // 控件
				id: 99,
				position: { // 控件位置
					left: 160,
					top: 120
				},
				iconPath: 'https://hellouniapp.dcloud.net.cn/static/location.png' // 控件图标
			}],
			address_info :"",
			address_info_recomd:"",
			address :""
		}
	},

地图组件 map | uni-app官网https://uniapp.dcloud.net.cn/component/map.html#地图组件用于展示地图,而定位API只是获取坐标,请勿混淆两者。

2.1 关于 map 组件各个属性的说明可以详见官方文档。

latitude::中心纬度
longitude:中心经度
markers:数组类型Array,标记点,用于标记你目前所处的位置。
scale:缩放级别,取值范围为3-20,默认16。高德地图缩放比例与微信小程序不同
controls:控件,可让你手指拖动地图时,它在你指定的位置不动
regionchange:视野发生变化时触发,依据这个函数的回调参数和控件来实时获取你选择的地址

tap:点击地图时触发; App-nvue、微信小程序2.9支持返回经纬度

 2.2 在使用 uni.getSetting() 方法来获取用户的当前设置,每次进入程序判断当前是否获得授权,如果没有,就去调起弹窗请求获得授权,如果获得授权,就直接获取当前地理位置。

3、实现调用地图组件方式一

3.1 首先在 onLoad 方法内触发 isGetLocation()方法

onLoad(){
		this.isGetLocation();
	},

 methods内调用判断是否获取授权方法和触发弹窗获得授权方法

isGetLocation(a = "scope.userLocation") { //检查当前是否已经授权访问scope属性
		    var _this = this;
		        uni.getSetting({
		            success(res) {
		                console.log(res)
		                if (!res.authSetting[a]) { //每次进入程序判断当前是否获得授权,如果没有就去获得授权,如果获得授权,就直接获取当前地理位置
		                    _this.getAuthorizeInfo()
		                } else {
		                     //方式一
		                     _this.getLocationInfo();
		                    // 方式二
		                    //_this.getLocation();
		                }
		            }
		        });
		},
		getAuthorizeInfo(a = "scope.userLocation") { // uniapp弹窗弹出获取授权(地理,个人微信信息等授权信息)弹窗
		    var _this = this;
		    uni.authorize({
		        scope: a,
		        success() { //允许授权
		            //方式一
		             _this.getLocationInfo();
					// 方式二
					//_this.getLocation();
		        }
		    })
		},

效果如下:

 点击允许即可调用地图组件选择地址:

3.2 点击右上角确定按钮时,getLocationInfo() 方法会返回具体位置信息。

getLocationInfo () {
		    uni.chooseLocation({
		        success: (res) => {
		            console.log(res)
					if(res.errMsg == "chooseLocation:ok"){
						this.address_info = res.name + res.address;
						this.latitude= res.latitude;
						this.longitude = res.longitude;
					}
		        }
		    })
		},

这种方式我们在获取用户授权后是直接通过 uni.chooseLocation() 方法选择的地理位置,相对简单。

 4、实现调用地图组件方式二

通过 uni.getLocation() 获取地理位置,就相对复杂一点儿。

4.1 先去获取当前所在位置的经纬度

getLocation(){
			uni.getLocation({
			    type: 'gcj02',
			    success: (res) => {
			        console.log(res)
			        this.latitude = res.latitude.toString()
			        this.longitude = res.longitude.toString()
			                        
			        // 获取地理位置详情信息
			        this.getLocationDetail()
			    }
			});
		},

 getLocation 返回参数如上图所示。

4.2 很据纬度获取详细的地址 

//根据经纬度获取详细的地址
		getLocationDetail () {
		    uni.request({
		        header: {
		            "Content-Type": "application/text"
		        },
		        url: 'https://apis.map.qq.com/ws/geocoder/v1/?location=' + this.latitude + ',' + this.longitude +
		                        '&key=XOXBZ-MZWWD-CDX4H-PONXN-UA5PJ-D7FJN',
		        success:(re)=> {
		            //成功获取到经纬度
		            console.log(re)
		            if (re.statusCode == 200) {
		                this.address_info_recomd = re.data.result.formatted_addresses.recommend
		                this.address_info = re.data.result.address_reference.town.title + re.data.result.address_reference.street.title + re.data.result.address_reference.landmark_l2.title
		                this.address = re.data.result.address
		                            
		            } else {
		                uni.showToast({
		                    title: '获取地理位置失败,请重试',
		                    icon: "none"
		                })
		            }
		        }
		    });
		}

 获取地理位置成功返回参数内容如上图所示。

4.3 再把控件定在地图的中心点,然后手动拖动地图,从而实现手动选择收货地址的功能。

regionchange (e) {
			console.log(e)
			if (e.type == 'end' && (e.causedBy == 'scale' || e.causedBy == 'drag')) {
				this.mapCtx = uni.createMapContext("mapSelected"); // 创建map的上下文对象, 从而操控map组件
				this.mapCtx.getCenterLocation({
					success: (res) => {
						console.log(res)
						this.latitude = res.latitude;
						this.longitude = res.longitude;
						this.getLocationDetail();
					}
				})
			}
		},

 当你手动拖动地图时,效果图如上。

mapSelected:map组件的id,结合以上方法去实时获取你选择位置的经纬度,根据经纬度即可实现实时选择收货地址的功能。

 5、完整代码示例 mapDemo.vue 内容如下:

<template>
    <view class="map_wrap">
        <map id="mapSelected" style="width: 690rpx; height: 300px; margin-left: 30rpx;" :latitude="latitude" :longitude="longitude" :markers="covers" :controls="controls" :scale="18" @controltap="controltap" @tap="clickMap" 
        @regionchange="regionchange">
        </map>
        
		<view class="mform">
			<view class="item">
				<view class="t">当前地址:</view>
				<view class="m">{{address}}</view>
			</view>
			<view class="item">
				<view class="t">推荐地址:</view>
				<view class="m">{{address_info}}</view>
			</view>
		</view>
        
        <!-- 保存按钮 -->
		<view class="btnbox">
			<button class="btn" @click="saveAddress()" style="width:85%;">保存</button>
		</view>
    </view>
</template>

<script>
export default {
	data() {
		return {
			title: 'map',
			latitude: 39.909,// 默认纬度
			longitude: 116.39742,// 默认经度(北京天安门)
			covers: [{
				id: 110, 
				latitude: 39.909,
				longitude: 116.39742,
				iconPath: '	https://hellouniapp.dcloud.net.cn/static/location.png',
				width: "20",
				height: "20"
			}],
			controls: [{ // 控件
				id: 99,
				position: { // 控件位置
					left: 160,
					top: 120
				},
				iconPath: 'https://hellouniapp.dcloud.net.cn/static/location.png' // 控件图标
			}],
			address_info :"",
			address_info_recomd:"",
			address :""
		}
	},
	onLoad(){
		this.isGetLocation();
	},
	methods: {
		isGetLocation(a = "scope.userLocation") { //检查当前是否已经授权访问scope属性
		    var _this = this;
		        uni.getSetting({
		            success(res) {
		                console.log(res)
		                if (!res.authSetting[a]) { //每次进入程序判断当前是否获得授权,如果没有就去获得授权,如果获得授权,就直接获取当前地理位置
		                    _this.getAuthorizeInfo()
		                } else {
		                    //方式一
		                    _this.getLocationInfo();
		                    // 方式二
		                    // _this.getLocation();
		                }
		            }
		        });
		},
		getAuthorizeInfo(a = "scope.userLocation") { // uniapp弹窗弹出获取授权(地理,个人微信信息等授权信息)弹窗
		    var _this = this;
		    uni.authorize({
		        scope: a,
		        success() { //允许授权
					//方式一
		            // _this.getLocationInfo();
					// 方式二
					_this.getLocation();
		        }
		    })
		},
		//点击地图时
		clickMap(e){
			console.log("点击地图时:"+e);
			this.latitude=e.detail.latitude;
			this.longitude = e.detail.longitude;
		},
		getLocationInfo () {
			//直接调用即可
		    uni.chooseLocation({
		        success: (res) => {
		            console.log(res)
					if(res.errMsg == "chooseLocation:ok"){
						this.address_info = res.name + res.address;
						this.latitude= res.latitude;
						this.longitude = res.longitude;
					}
		        }
		    })
		},
		//获取当前所在位置的经纬度
		getLocation(){
			uni.getLocation({
			    type: 'gcj02',
			    success: (res) => {
			        console.log(res)
			        this.latitude = res.latitude.toString()
			        this.longitude = res.longitude.toString()
			                        
			        // 获取地理位置详情信息
			        this.getLocationDetail()
			    }
			});
		},
		//根据经纬度获取详细的地址
		getLocationDetail () {
		    uni.request({
		        header: {
		            "Content-Type": "application/text"
		        },
		        url: 'https://apis.map.qq.com/ws/geocoder/v1/?location=' + this.latitude + ',' + this.longitude +
		                        '&key=XOXBZ-MZWWD-CDX4H-PONXN-UA5PJ-D7FJN',
		        success:(re)=> {
		            //成功获取到经纬度
		            console.log(re)
		            if (re.statusCode == 200) {
		                this.address_info_recomd = re.data.result.formatted_addresses.recommend
		                this.address_info = re.data.result.address_reference.town.title + re.data.result.address_reference.street.title + re.data.result.address_reference.landmark_l2.title
		                this.address = re.data.result.address
		                            
		            } else {
		                uni.showToast({
		                    title: '获取地理位置失败,请重试',
		                    icon: "none"
		                })
		            }
		        }
		    });
		},
		// 在地图渲染更新完成时触发
		regionchange (e) {
			console.log(e)
			if (e.type == 'end' && (e.causedBy == 'scale' || e.causedBy == 'drag')) {
				this.mapCtx = uni.createMapContext("mapSelected"); // 创建map的上下文对象, 从而操控map组件
				this.mapCtx.getCenterLocation({
					success: (res) => {
						console.log(res)
						this.latitude = res.latitude;
						this.longitude = res.longitude;
						this.getLocationDetail();
					}
				})
			}
		},
	}
}
</script>
<style>

</style>

6、结尾

 以上就是两种调用地图组件,确定地理位置的方式,如您在使用过程中有所疑惑,可以私信我,咱们共同交流解决问题。这里页面样式我并没有太注意,毕竟我这个只是案例代码分享。

如果感觉小编的分享对您有所帮助的话,还请点赞、收藏 + 关注哦^_^

 

  • 34
    点赞
  • 128
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 15
    评论
uni-app是一款基于Vue.js框架的跨平台开发工具,可以同时开发微信小程序、H5、App等多个平台的应用。在uni-app,我们可以很方便地使用地图组件来标记点。 首先,我们需要引入uni-app官方提供的地图组件,在页面的json文件添加以下代码: ``` { "usingComponents": { "uni-map": "@dcloudio/uni-map/uni-map" } } ``` 然后,在需要使用地图的页面,在template添加以下代码: ``` <template> <view> <uni-map :longitude="longitude" :latitude="latitude" :markers="markers" :include-points="true" ></uni-map> </view> </template> ``` 在script,我们需要定义地图的经纬度和标记点的数据: ``` <script> export default { data() { return { longitude: 113.324520, latitude: 23.099994, markers: [{ id: 1, longitude: 113.324520, latitude: 23.099994, title: '标记点1', iconPath: '/static/marker.png', width: 30, height: 30 }, { id: 2, longitude: 113.326520, latitude: 23.099994, title: '标记点2', iconPath: '/static/marker.png', width: 30, height: 30 }] } } } </script> ``` 我们可以通过设置longitude和latitude来指定地图心点,通过markers来设置标记点的位置、标题、图标等信息。iconPath需要提前准备好对应的图标文件。 最后,在地图组件上设置:include-points="true",可以使得地图自动包含所有标记点,确保能够显示所有标记点。 以上就是使用uni-app来在微信小程序标记点的方法。通过引入uni-app提供的地图组件,结合相关的属性和数据即可实现地图的标记点功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 15
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

跟着飞哥学编程

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值