uniapp 使用定位示例詳解
前言
業(yè)務(wù)需要用定位功能,還是持續(xù)后臺定位的,所以研究了一下,深入淺出好吧,大伙直接復制粘貼拿去用就行!我把他分為在微信小程序和APP中的情況, 先發(fā)一波APP的,點贊超過10我就發(fā)個微信小程序的。
第一步
在manifest.jsop中復制這段代碼,xxx是你自己的信息哦,有了這段代碼,前臺定位和后臺定位權(quán)限都可以了。
"mp-weixin" : {
"permission" : {
"scope.userLocation" : {
"desc" : "xxx" //描述用來干啥的
}
},
"requiredBackgroundModes" : [ "location" ],
"requiredPrivateInfos" : [
"getLocation",
"onLocationChange",
"startLocationUpdate",
"startLocationUpdateBackground"
]
},
第二步
在要用的地方寫 , 這個是檢測手機定位打開沒有的代碼
// #ifdef APP-PLUS
// 獲取是否開啟定位
let system = uni.getSystemInfoSync(); // 獲取系統(tǒng)信息
if (system.platform === 'android') { // 判斷平臺
var context = plus.android.importClass("android.content.Context");
var locationManager = plus.android.importClass("android.location.LocationManager");
var main = plus.android.runtimeMainActivity();
var mainSvr = main.getSystemService(context.LOCATION_SERVICE);
if (!mainSvr.isProviderEnabled(locationManager.GPS_PROVIDER)) {
var main = plus.android.runtimeMainActivity();
var Intent = plus.android.importClass('android.content.Intent');
var Settings = plus.android.importClass('android.provider.Settings');
var intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
main.startActivity(intent); // 打開系統(tǒng)設(shè)置GPS服務(wù)頁面
}
} else if (system.platform === 'ios') {
var cllocationManger = plus.ios.import("CLLocationManager");
var enable = cllocationManger.locationServicesEnabled();
var status = cllocationManger.authorizationStatus();
plus.ios.deleteObject(cllocationManger);
console.log("手機系統(tǒng)的定位沒有打開");
uni.showModal({
title: '提示',
content: '請打開定位服務(wù)功能',
showCancel: false, // 不顯示取消按鈕
success() {
var UIApplication = plus.ios.import("UIApplication");
var application2 = UIApplication.sharedApplication();
var NSURL2 = plus.ios.import("NSURL");
var setting2 = NSURL2.URLWithString("App-Prefs:root=Privacy&path=LOCATION");
application2.openURL(setting2);
plus.ios.deleteObject(setting2);
plus.ios.deleteObject(NSURL2);
plus.ios.deleteObject(application2);
}
});
}
// #endif
第三步
開始拿定位 ,用uni的方法
uni.getLocation({success(res)=>{
console.log('當前位置的經(jīng)度:' + res.longitude);
console.log('當前位置的緯度:' + res.latitude);
console.log('當前位置的速度:' + res.speed);
console.log('當前位置的精確度:' + res.accuracy);
}) 以上就是uniapp 使用定位示例詳解的詳細內(nèi)容,更多關(guān)于uniapp 定位使用的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
javascript代碼在ie8里報錯 document.getElementById(...) 為空或不是對象的解決方
今天更升級了ie8,發(fā)現(xiàn)原來在ie7下可以運行的代碼,不能運行了,發(fā)現(xiàn)了一些細節(jié),附臨時修改辦法。2009-11-11
JavaScript this關(guān)鍵字指向常用情況解析
這篇文章主要介紹了JavaScript this關(guān)鍵字指向常用情況解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-09-09
前端js實現(xiàn)文件的斷點續(xù)傳 后端PHP文件接收
這篇文章主要為大家詳細介紹了斷點續(xù)傳的簡單例子,前端文件提交,后端PHP文件接收,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-10-10

