uniapp h5 obtains the user's geographic location information (Gaode map)

 Use uni.getLocation() to first get the latitude and longitude of the current location information

H5 end test can use http, online packaging needs to be set to https mode

Google Chrome may not be able to obtain any information, because the location information of Google Chrome is obtained by connecting to the Google server, and domestic users may fail to obtain location information

getCurrentlocation(){
    let that = this
	uni.getLocation({
	type: 'wgs84',
	isHighAccuracy: true,//开启高精度定位
	success(res) {
	console.log('当前位置的经度:' + res.longitude);
	console.log('当前位置的纬度:' + res.latitude);
	}
	})
}

Use Gaode development platform to register a key

Amap Development Platform: Amap Open Platform | Amap API (amap.com)

 Get the key and configure it in manifest.json

 After the configuration is complete, use Gaode's inverse geocoding to parse the latitude and longitude obtained from the above request

	turnAdrr(longitude, latitude) {
				let that = this
				let _key = '高德里你注册的key'; //高德API key类型:web服务
				uni.request({
					method: 'GET',
					url: 'https://restapi.amap.com/v3/geocode/regeo?parameters',
					data: {
						key: _key,
						location: `${longitude},${latitude}`,
						output: 'JSON',
					},
					success: (res) => {
						console.log(res.data.regeocode.formatted_address);
				           //用户所在的地理位置信息
					},
					fail: r => {
						console.log(r);
					}
				});
			}

For App, you can directly call uni.getLocation() to get the user's location without parsing, you can get it directly

uni.getLocation({
    type: 'gcj02', //app直接获取地理位置信息要使用gcj02
    geocode:true , //必须要将geocode配置为true
	isHighAccuracy: true,
	success(res) {
		console.log(res.address)
	},
    fail(err){
    console.log(err)
}
})

Guess you like

Origin blog.csdn.net/w_z_bin/article/details/129144955