uniapp 使用定位示例詳解
前言
業(yè)務(wù)需要用定位功能,還是持續(xù)后臺(tái)定位的,所以研究了一下,深入淺出好吧,大伙直接復(fù)制粘貼拿去用就行!我把他分為在微信小程序和APP中的情況, 先發(fā)一波APP的,點(diǎn)贊超過10我就發(fā)個(gè)微信小程序的。
第一步
在manifest.jsop中復(fù)制這段代碼,xxx是你自己的信息哦,有了這段代碼,前臺(tái)定位和后臺(tái)定位權(quán)限都可以了。
"mp-weixin" : { "permission" : { "scope.userLocation" : { "desc" : "xxx" //描述用來干啥的 } }, "requiredBackgroundModes" : [ "location" ], "requiredPrivateInfos" : [ "getLocation", "onLocationChange", "startLocationUpdate", "startLocationUpdateBackground" ] },
第二步
在要用的地方寫 , 這個(gè)是檢測(cè)手機(jī)定位打開沒有的代碼
// #ifdef APP-PLUS // 獲取是否開啟定位 let system = uni.getSystemInfoSync(); // 獲取系統(tǒng)信息 if (system.platform === 'android') { // 判斷平臺(tái) 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("手機(jī)系統(tǒng)的定位沒有打開"); uni.showModal({ title: '提示', content: '請(qǐng)打開定位服務(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('當(dāng)前位置的經(jīng)度:' + res.longitude); console.log('當(dāng)前位置的緯度:' + res.latitude); console.log('當(dāng)前位置的速度:' + res.speed); console.log('當(dāng)前位置的精確度:' + res.accuracy); })
以上就是uniapp 使用定位示例詳解的詳細(xì)內(nèi)容,更多關(guān)于uniapp 定位使用的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
微信小程序返回上一級(jí)頁面的實(shí)現(xiàn)代碼
這篇文章主要介紹了微信小程序返回上一級(jí)頁面的實(shí)現(xiàn)代碼,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06JS驗(yàn)證IP,子網(wǎng)掩碼,網(wǎng)關(guān)和MAC的方法
這篇文章主要介紹了JS驗(yàn)證IP,子網(wǎng)掩碼,網(wǎng)關(guān)和MAC的方法,涉及javascript正則表達(dá)式的相關(guān)使用技巧,需要的朋友可以參考下2015-07-07微信小程序定義和調(diào)用全局變量globalData的實(shí)現(xiàn)
這篇文章主要介紹了微信小程序定義和調(diào)用全局變量globalData的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11