首先申请地图的key,然后在manifest.json文件中配置,如下图
下面直接上代码
getLocation: function() {
let that = this
uni.getLocation({
type: 'wgs84',
geocode: true,
success: function(res) {
var point = new plus.maps.Point(res.longitude, res.latitude);
plus.maps.Map.reverseGeocode(
point,
{},
function(event) {
var address = event.address; // 转换后的地理位置
var point = event.coord; // 转换后的坐标信息
var coordType = event.coordType; // 转换后的坐标系类型
console.log(address, 'address');
var reg = /.+?(省|市|自治区|自治州|县|区)/g;
console.log(address.match(reg));
let addressList=address.match(reg).toString().split(",");
let cityTitle= addressList[1];
if (cityTitle.length > 0) {
let lastChares = cityTitle.substring(cityTitle.length - 1);
if (lastChares == "市") {
cityTitle = cityTitle.substring(0, cityTitle.length - 1);
}
// 全局定义触发的事件
uni.$emit('upadteLoaction', cityTitle)
}
},
function(e) {}
);
},
fail: (err) => {
console.log('err ==>',err);
}
});
},