首先配置manifest.json源码里的 mp-weixin 项

	"mp-weixin": {
		"appid": "wxea949aee023c86f5",
		"setting": {
			"urlCheck": false,
			"es6": true,
			"postcss": true,
			"minified": true
		},
		"usingComponents": true,
		"permission": {
			"scope.userLocation": {
				"desc": "4513"
			}
		},
		
	"requiredPrivateInfos": ["chooseLocation", "getLocation"]

	},

注意 "requiredPrivateInfos": ["chooseLocation", "getLocation"] 这个之前版本是默认就可以用的 现在得必须配置对应得函数api才行

其次

	uni.getLocation({
			    type: 'wgs84',
			    geocode:true,//设置该参数为true可直接获取经纬度及城市信息
			    success: function (res) {
			        console.log('获取定位信息',res);
			        
			        // 创建地图坐标对象
			        // var point = new plus.maps.Point(res.longitude, res.latitude);
			        // //静态方法,反向地理编码
			        // plus.maps.Map.reverseGeocode(point, {},(event)=>{
			        //     var address = event.address; // 转换后的地理位置
			        //     var point = event.coord; // 转换后的坐标信息
			        //     var coordType = event.coordType; // 转换后的坐标系类型
			        //     var reg = /.+?(省|市|自治区|自治州|县|区)/g;
			        //     var addressList = address.match(reg).toString().split(",");
			        //     //注意 因为存在直辖市, 当所在地区为普通省市时,addressList.length == 3,city = addressList[1];当所在地区为直辖市时addressList.length == 2,city = addressList[0];
			        //     let city = addressList.length == 3?addressList[1]:addressList[0];
			        //     console.log("addressList",addressList);
			        //     },
			        // 	function(e) {
			        //     	console.log("失败回调",e);
			        // 	}
			        // );
			    },
			    fail: function (err) {
			        console.log("获取定位失败",err);
			        uni.showToast({
			            title: '获取定位失败',
			            icon:'none'
			        });
			    }
			});

更多推荐