uni-app微信小程序结合腾讯地图获取定位导航以及城市选择器

目录

第一步:登录小程序公众平台==>设置==>第三方设置

 第二步:登录腾讯地图申请属于自己小程序的key

 第三步:找到腾讯地图的插件​​​​​​​

 第四步:添加插件与允许授权

 第五步:使用


第一步:登录小程序公众平台==>设置==>第三方设置

 

 第二步:登录腾讯地图申请属于自己小程序的key

 腾讯地图后台:https://lbs.qq.com/dev/console/application/mine

 

 添加key,授权使用的小程序appId

 第三步:找到腾讯地图的插件

 插件文档:https://lbs.qq.com/miniProgram/plugin/pluginGuide/pluginOverview

 第四步:添加插件与允许授权

    "plugins" : {
            "chooseLocation" : {
                "version" : "1.0.9",
                "provider" : "wx76a9a06e5b4e693e"
            },
            "citySelector" : {
                "version" : "1.0.1",
                "provider" : "wx63ffb7b7894e99ae"
            }
        },

 第五步:使用

1.地图中选择位置

    const key = ""; //使用在腾讯位置服务申请的key
	const referer = ''; //调用插件的app的名称
	const location = JSON.stringify({
		// 修改时回显位置
		latitude: this.form.lat || this.location.latitude,
		longitude: this.form.lon || this.location.longitude
	});
	const category = '生活服务,娱乐休闲';
	uni.navigateTo({
		url: 'plugin://chooseLocation/index?key=' + key + '&referer=' + referer + '&location=' +
			location + '&category=' + category
	});

返回结果接收

const chooseLocation = requirePlugin('chooseLocation')
		
onShow() {
		const location = chooseLocation.getLocation(); // 如果点击确认选点按钮,则返回选点结果对象,否则返回null
		console.log(location)
		if (location) {
			this.form.actiPlace = location.name
			this.form.actiCity = location.city
			this.form.lat = location.latitude
			this.form.lon = location.longitude
		}
	},
	onUnload() {
		// 页面卸载时设置插件选点数据为null,防止再次进入页面,geLocation返回的是上次选点结果
		chooseLocation.setLocation(null);
	},

2.城市列表选择

	const key = ''; //使用在腾讯位置服务申请的key
	const referer = ''; //调用插件的app的名称
	const hotCitys = ''; // 用户自定义的的热门城市
	uni.navigateTo({	              
       url:`plugin://citySelector/indexkey=${key}&referer=${referer}&hotCitys=${hotCitys}`,
	})

返回结果接收

const citySelector = requirePlugin('citySelector')

onShow() {
	const selectedCity = citySelector.getCity(); // 选择城市后返回城市信息对象,若未选择返回null
	console.log(selectedCity)
	if (selectedCity) {
		let cityInfo = {
			city: selectedCity.fullname,
			latitude: selectedCity.location.latitude,
			longitude: selectedCity.location.longitude
		}
},
onUnload() {
	// 页面卸载时清空插件数据,防止再次进入页面,getCity返回的是上次的结果
	citySelector.clearCity();
},

 更多操作请参考腾讯文档:https://lbs.qq.com/miniProgram/plugin/pluginGuide/pluginOverview

  • 3
    点赞
  • 41
    收藏
    觉得还不错? 一键收藏
  • 12
    评论
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提供的地图组件,结合相关的属性和数据即可实现地图的标记点功能。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值