//APP 就可以直接获取到
// #ifdef APP-PLUS
uni.getLocation({
type: 'gcj02',
geocode: true,
highAccuracyExpireTime: 3000,
success: function(res) {
that.longitude = res.longitude; // 经度
that.latitude = res.latitude; // 纬度
console.log('当前位置的经度:' + res.longitude);
console.log('当前位置的纬度:' + res.latitude);
console.log('当前地址:' + res.address.city);
// console.log(that.address)
that.address = res.address.district
}
});
// #endif
计算两点之间的距离(两个经纬度)
rad(d) {
return d * Math.PI / 180.0;
},
/**
* 计算两点间直线距离
* @param a 表示纬度差
* @param b 表示经度差
* @return 返回的是距离,单位m
*/
getDistance(latFrom, lngFrom, latTo, lngTo) {
var radLatFrom = this.rad(latFrom);
var radLatTo = this.rad(latTo);
var a = radLatFrom - radLatTo;
var b = this.rad(lngFrom) - this.rad(lngTo);
var distance = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLatFrom) * Math.cos(radLatTo) * Math.pow(Math.sin(b / 2), 2)));
distance = distance * EARTH_RADIUS;
distance = Math.round(distance * 10000) / 10000;
return parseFloat(distance.toFixed(0));
},
手动点击刷新
<template>
<view>
<view v-for="(item,index) of list" :key="index">
{{item}}
</view>
<button @click="pullDown">点击触发下拉刷新</button>
</view>
</template>
<script>
export default {
data() {
return {
list: [1, 2, 3, 4, 5]
}
},
methods: {
pullDown() {
//触发下拉刷新
uni.startPullDownRefresh()
}
},
onPullDownRefresh() {
console.log("触发下拉刷新")
setTimeout(() => {
this.list = [1, 2, 3, 5, 3, 2]
//关闭下拉刷新
uni.stopPullDownRefresh()
}, 2000)
}
}
</script>
<style>
</style>
配置刷新
所对应的函数
<template>
<view>
<view v-for="(item,index) of list" :key="index">
{{item}}
</view>
</view>
</template>
<script>
export default {
data() {
return {
list: [1, 2, 3, 4, 5]
}
},
methods: {},
onPullDownRefresh() {
console.log("触发下拉刷新")
setTimeout(() => {
this.list = [1, 2, 3, 5, 3, 2]
//关闭下拉刷新
uni.stopPullDownRefresh()
},2000)
}
}
</script>
<style>
</style>
本页面返回上一个页面的刷新
let pages = getCurrentPages(); // 当前页面
let beforePage = pages[pages.length - 2]; // 上一页
setTimeout(() => {
uni.navigateBack({
delta: 1,
success: function() {
beforePage.$vm.reFresh(); // 执行前一个页面的刷新
}
});
}, 1000);
内置地图
uni.openLocation({
address: value.stationName,
longitude: Number(value.coordinateX),
latitude: Number(value.coordinateY),
fail(error) {
uni.showToast({
icon: 'error',
title: '打开地图失败'
});
}
});
自动返回上一页并进行刷新
let pages = getCurrentPages(); // 当前页面
let beforePage = pages[pages.length - 2]; // 上一页
setTimeout(() => {
uni.navigateBack({
delta: 1,
success: function() {
beforePage.$vm.reFresh(); // 执行前一个页面的刷新
}
})
}, 1000)
位置详情网址
https://uniapp.dcloud.net.cn/api/location/location.html#getlocation