uniapp如何实现精准定位并展示附近信息的功能?

2026-04-02 23:061阅读0评论SEO基础
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计594个文字,预计阅读时间需要3分钟。

uniapp如何实现精准定位并展示附近信息的功能?

目录+前言+第一步+第二步+第三步+前言+业务需求定位功能,持续后端定位,深入研究浅出,大伙直接复制粘贴用就完事!我把他分为微信小程序和APP中的定位功能。

目录
  • 前言
  • 第一步
  • 第二步
  • 第三步

前言

业务需要用定位功能,还是持续后台定位的,所以研究了一下,深入浅出好吧,大伙直接复制粘贴拿去用就行!我把他分为在微信小程序和APP中的情况, 先发一波APP的,点赞超过10我就发个微信小程序的。

uniapp如何实现精准定位并展示附近信息的功能?

第一步

在manifest.jsop中复制这段代码,xxx是你自己的信息哦,有了这段代码,前台定位和后台定位权限都可以了。

"mp-weixin" : { "permission" : { "scope.userLocation" : { "desc" : "xxx" //描述用来干啥的 } }, "requiredBackgroundModes" : [ "location" ], "requiredPrivateInfos" : [ "getLocation", "onLocationChange", "startLocationUpdate", "startLocationUpdateBackground" ] },

第二步

在要用的地方写 , 这个是检测手机定位打开没有的代码

// #ifdef APP-PLUS // 获取是否开启定位 let system = uni.getSystemInfoSync(); // 获取系统信息 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); // 打开系统设置GPS服务页面 } } 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("手机系统的定位没有打开"); uni.showModal({ title: '提示', content: '请打开定位服务功能', 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('当前位置的经度:' + res.longitude); console.log('当前位置的纬度:' + res.latitude); console.log('当前位置的速度:' + res.speed); console.log('当前位置的精确度:' + res.accuracy); })

以上就是uniapp 使用定位示例详解的详细内容,更多关于uniapp 定位使用的资料请关注自由互联其它相关文章!

本文共计594个文字,预计阅读时间需要3分钟。

uniapp如何实现精准定位并展示附近信息的功能?

目录+前言+第一步+第二步+第三步+前言+业务需求定位功能,持续后端定位,深入研究浅出,大伙直接复制粘贴用就完事!我把他分为微信小程序和APP中的定位功能。

目录
  • 前言
  • 第一步
  • 第二步
  • 第三步

前言

业务需要用定位功能,还是持续后台定位的,所以研究了一下,深入浅出好吧,大伙直接复制粘贴拿去用就行!我把他分为在微信小程序和APP中的情况, 先发一波APP的,点赞超过10我就发个微信小程序的。

uniapp如何实现精准定位并展示附近信息的功能?

第一步

在manifest.jsop中复制这段代码,xxx是你自己的信息哦,有了这段代码,前台定位和后台定位权限都可以了。

"mp-weixin" : { "permission" : { "scope.userLocation" : { "desc" : "xxx" //描述用来干啥的 } }, "requiredBackgroundModes" : [ "location" ], "requiredPrivateInfos" : [ "getLocation", "onLocationChange", "startLocationUpdate", "startLocationUpdateBackground" ] },

第二步

在要用的地方写 , 这个是检测手机定位打开没有的代码

// #ifdef APP-PLUS // 获取是否开启定位 let system = uni.getSystemInfoSync(); // 获取系统信息 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); // 打开系统设置GPS服务页面 } } 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("手机系统的定位没有打开"); uni.showModal({ title: '提示', content: '请打开定位服务功能', 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('当前位置的经度:' + res.longitude); console.log('当前位置的纬度:' + res.latitude); console.log('当前位置的速度:' + res.speed); console.log('当前位置的精确度:' + res.accuracy); })

以上就是uniapp 使用定位示例详解的详细内容,更多关于uniapp 定位使用的资料请关注自由互联其它相关文章!