# uni.chooseLocation - 选择位置示例 ## 官方文档 参考官方文档:https://uniapp.dcloud.net.cn/api/location/choose-location.html#chooselocation ## 概述 `uni.chooseLocation` 用于打开地图选择位置。 ## 基础用法 ```javascript uni.chooseLocation({ success: (res) => { console.log('选择的位置', res.name, res.address) } }) ``` ## 完整示例 ### 示例 1: 基本选择位置 ```javascript uni.chooseLocation({ success: (res) => { console.log('位置名称', res.name) console.log('详细地址', res.address) console.log('纬度', res.latitude) console.log('经度', res.longitude) }, fail: (err) => { console.error('选择失败', err) } }) ``` ### 示例 2: 在页面中使用 ```vue ``` ### 示例 3: 选择收货地址 ```vue ``` ### 示例 4: 检查权限 ```javascript function chooseLocationWithPermission() { // 先检查权限 uni.getSetting({ success: (res) => { if (res.authSetting['scope.userLocation']) { // 已授权,直接选择 chooseLocation() } else { // 请求授权 uni.authorize({ scope: 'scope.userLocation', success: () => { chooseLocation() }, fail: () => { uni.showModal({ title: '提示', content: '需要位置权限才能选择位置', success: (modalRes) => { if (modalRes.confirm) { uni.openSetting() } } }) } }) } } }) } function chooseLocation() { uni.chooseLocation({ success: (res) => { console.log('选择的位置', res) } }) } ``` ### 示例 5: 在地图上显示选择的位置 ```vue ``` ## 返回值 | 参数名 | 类型 | 说明 | |--------|------|------| | name | String | 位置名称 | | address | String | 详细地址 | | latitude | Number | 纬度 | | longitude | Number | 经度 | ## 平台兼容性 | 平台 | 支持情况 | |------|---------| | H5 | ❌ | | 微信小程序 | ✅ | | 支付宝小程序 | ✅ | | 百度小程序 | ✅ | | 字节跳动小程序 | ✅ | | QQ 小程序 | ✅ | | 快手小程序 | ✅ | | App | ✅ | | 快应用 | ✅ | ## 注意事项 1. 需要用户授权位置权限 2. H5 平台不支持此 API 3. 选择的位置包含名称、地址和坐标信息 4. 建议在需要时再请求权限 ## 参考资源 - **官方文档**: https://uniapp.dcloud.net.cn/api/location/choose-location.html#chooselocation - **获取位置**: https://uniapp.dcloud.net.cn/api/location/location.html#getlocation - **打开地图**: https://uniapp.dcloud.net.cn/api/location/open-location.html#openlocation