前期准备
-
注册一个腾讯地图的key
在注册腾讯地图的key之前,首先需要获得一个appid,注册key的步骤如下: www.kancloud.cn/jibinnet/qi…
-
在app.config.js中配置如下信息(2022.7.14日以后需要配置)
"permission": {
"scope.userLocation": {
"desc": "为了更好的服务体验,我们希望获取你的位置"
}
}
- 代码如下
class App extends Component {
getUserAddressInfo = () =>{
//获取用户地理位置信息
Taro.getLocation({
type:'gcj02',
success:res=>{
let {latitude,longitude} = res
console.log(res)
this.getUserAddress(latitude,longitude).then(res=>{
console.log('res地理位置',res)
})
}
})
}
getUserAddress = (latitude,longitude) =>{
//根据经纬度获取用户地址
return new Promise((resolve,reject)=>{
Taro.request({
url:`https://apis.map.qq.com/ws/geocoder/v1/?location=${latitude},${longitude}&key=DXOBZ-IBW36-6DXSH-M57FU-VW4WV-PABRI`
}).then(res=>{
resolve(res)
})
})
}
componentDidMount () {
this.getUserAddressInfo()
}
render(){
return (
<></>
)
}
}