小程序调用定位的组件需要配置

190 阅读1分钟

1调用腾讯位置相关api和地图选点服务

1、登录微信公众平台 设置——第三方设置——插件管理——添加插件——搜索对应插件名称

微信截图_20230213111643.png 2、在小程序配置中添加下面代码

"mp-weixin": {
		"appid": "wxd996f0fe27e5a3ec",
		"setting": {
			"urlCheck": false,
			"es6": true,
			"postcss": true,
			"minified": true
		},
		"usingComponents": true,
		"permission": {
			"scope.userLocation": {
				"desc": "你的位置信息将用于小程序位置接口的效果展示"
			}
		},
		"requiredBackgroundModes": ["audio", "location"],
		"requiredPrivateInfos": ["getLocation", "onLocationChange", "startLocationUpdate","chooseLocation"]
	},

getLocation用于获取用户当前位置

uni.getLocation({
    type: 'wgs84',
    success: function(res) {}					
    fail: () => {}
});

chooseLocation用于地图选点

uni.chooseLocation({
    latitude:30,  //目标地纬度(不必填)
    longitude:120,  //目标地纬度(不必填)
    success:()=>{}, //成功回调
    fail:()=>{},  //失败回调
})

onLocationChange 监听位置改变api 配和startLocationUpdate使用 一般用法

uni.startLocationUpdate({
    success() {
        uni.onLocationChange(function(res) {
            console.log(res)
            //位置改变要执行的逻辑
        });
    }
})