uni-app開發(fā)小程序使用uni.getLocation()實現(xiàn)頁面即時獲取當前定位信息demo
前端  /  管理員 發(fā)布于 3年前   17178
uniapp開發(fā)百度小程序實現(xiàn)實時獲取當前定位功能,接上一篇uni-app文章
uni.getLocation:獲取當前的地理位置、速度。
官方手冊:
https://uniapp.dcloud.io/api/location/location?id=getlocation
視圖省略...
js代碼:
<script>
from '@/utils/utils.js'
let timer;
export default {
data() {
return {
getPlistData: { //參數(shù)
lat:'',
lng:'',
city:''
},
};
},
components: {
},
onReachBottom() {
},
onLoad(e:any) {
let that = this;
},
onHide() {
clearInterval(timer)
console.log("頁面隱藏onHide")
},
onShow() {
let that:any = this;
that.getContactsApi()
console.log("頁面onShow")
clearInterval(timer)
timer = setInterval(function() {
that.getContactsApi()
}, 30000)
},
methods: {
getContactsApi():void {
let that:any = this;
that.getLocationAuth(false);
},
getLocationAuth(flag:any = false):void {
let that:any = this;
uni.getLocation({
type: 'wgs84',
altitude: true,
success: function(res:any) {
console.log(res, "經(jīng)緯度授權")
if (that.getPlistData.lat === res.latitude && that.getPlistData.lng === res.longitude) {
console.log('位置相同,不請求數(shù)據(jù)')
return
} else {
console.log('位置')
}
that.addressTop = "定位中..."
that.getPlistData.lat = res.latitude;
that.getPlistData.lng = res.longitude;
getMapSdk.locationCity(res.latitude, res.longitude).then(res => {
console.log(res, "當前定位--------------------------------------");
that.addressTop = res.result.address
if (flag) {
var str = res.city.result.address_component
str = str.substring(0, res.city.length - 1);
that.getPlistData.city = str;
}
that.getData();
})
},
fail: function(res:any) {
console.log(res, "經(jīng)緯度沒有授權")
uni.getSetting({
success: (res:any) => {
let authSetting = res.authSetting;
if (!res.authSetting['scope.userLocation']) {
uni.showModal({
title: '您未開啟地理位置授權',
content: '小程序將無法正常使用',
success: (res:any)=> {
if (res.confirm) {
uni.openSetting()
}
}
})
} else {
uni.showModal({
title: '您未開啟手機地理位置',
content: '小程序將無法正常使用',
success: (res:any) => {
if (res.confirm) {
uni.openSetting()
}
}
})
}
}
})
}
});
},
},
computed: {
}
}
</script>
css省略...
看看效果圖:
獲取定位詳細數(shù)據(jù)
實時獲取定位數(shù)據(jù)
完
123 在
Clash for Windows作者刪庫跑路了,github已404中評論 按理說只要你在國內,所有的流量進出都在監(jiān)控范圍內,不管你怎么隱藏也沒用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最簡單的方法中評論 好久好久都沒看友情鏈接申請了,今天剛看,已經(jīng)添加。..博主 在
佛跳墻vpn軟件不會用?上不了網(wǎng)?佛跳墻vpn常見問題以及解決辦法中評論 @1111老鐵這個不行了,可以看看近期評論的其他文章..1111 在
佛跳墻vpn軟件不會用?上不了網(wǎng)?佛跳墻vpn常見問題以及解決辦法中評論 網(wǎng)站不能打開,博主百忙中能否發(fā)個APP下載鏈接,佛跳墻或極光..路人 在
php中使用hyperf框架調用訊飛星火大模型實現(xiàn)國內版chatgpt功能示例中評論 教程很詳細,如果加個前端chatgpt對話頁面就完美了..
Copyright·? 2019 侯體宗版權所有·
粵ICP備20027696號