要在高德地图开放平台申请key
下载 amap-wx.130.js
manifest.json
<template>
<view class="dingwei">
<view class="dingwei_city">
<view class="dingwei_city_one" @tap="back_city(position,true)">
{{position?position.city:'定位失败'}}
</view>
</view>
</view>
</template>
<script>
import amap from '@/common/amap-wx.130.js'
export default {
data() {
return {
position: [], //定位获取的位置
}
},
onShow() {
this.getWarpweft()
},
methods: {
getWarpweft() {
const that = this;
uni.getLocation({
type: 'wgs84', // 或 'gcj02'
success(res) {
console.log('定位成功', res);
const latitude = res.latitude;
const longitude = res.longitude;
// 调用逆地理编码方法获取地名
var myAmapFun = new amap.AMapWX({
key: '高德地图申请的key'
});
myAmapFun.getRegeo({
location: longitude + ',' + latitude,
success: function(data) {
// console.log(data[0],99);
// that.position = data[0].regeocodeData.addressComponent.city;
that.position = data[0].regeocodeData.addressComponent;
console.log(data[0].regeocodeData.addressComponent.city);
//成功回调
},
fail: function(info) {
//失败回调
console.log(info)
}
})
},
fail(err) {
console.log('定位失败', err);
// 处理定位失败的情况,例如提示用户或执行其他操作
},
});
}
}
}
</script>
<style>
</style>