uni-app:获取当前经纬度解决方案+如何布置全局组件 - 林恒
source link: https://www.cnblogs.com/smileZAZ/p/17152325.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
这里给大家分享我在网上总结出来的一些知识,希望对大家有所帮助
一.布置全局组件
在我们开发的过程中,会碰到一个现象,就是在页面里面引入组件,总算要写import,components才能引用,这里给大家分享我们的一个解决方案
1.首先要建立一个components文件夹,用来放我们的所有组件
2.然后在里面写好组件
3.来到main.js,在代码中加入两行代码
import movable from "@/components/movable/index.vue"; Vue.component("movable", movable); |
这样我们就能在页面里,不用写import,components,就能引用了
二.获取当前经纬度解决方案
这里给大家分享出一套我使用的获取当前经纬度的方案
1.小程序设置,去小程序公众平台,开启接口权限
2.代码中manifest.json文件以下位置加上代码
/* 小程序特有相关 */ "mp-weixin" : { "appid" : "", "setting" : { "urlCheck" : false }, "usingComponents" : true, "permission" : { "scope.userLocation" : { "desc" : "你的位置信息将用于和门店的距离长度" } }, "requiredPrivateInfos" : [ "chooseLocation", "getLocation" ] }, |
3.页面方法分享,分为检测权限,成功处理,错误处理
// 位置授权 getAuthorizeInfo() { const that = this; uni.authorize({ scope: 'scope.userLocation', success() { // 允许授权 that.getLocationInfo(); }, fail() { // 拒绝授权 that.openConfirm(); // console.log("你拒绝了授权,无法获得周边信息") } }) }, |
获取地理位置
// 获取地理位置 getLocationInfo() { const that = this uni.getLocation({ type: 'wgs84', success(res) { uni.setStorageSync("lat", res.latitude) uni.setStorageSync("lng", res.longitude) }, fail(res) { // 拒绝授权 console.log(res, '222'); } }); }, |
// 再次获取授权 // 当用户第一次拒绝后再次请求授权 openConfirm() { uni.showModal({ title: '请求授权当前位置', content: '需要获取您的地理位置,请确认授权', showCancel: false, success: (res) => { if (res.confirm) { uni.openSetting(); // 打开地图权限设置 } } }); }, |
如果对您有所帮助,欢迎您点个关注,我会定时更新技术文档,大家一起讨论学习,一起进步。
__EOF__
Recommend
-
47
通过XSS获取经纬度
-
18
灯光布置学这4招,不仅省电费,还让你家的家具看上去贵10倍
-
13
来自: rovaniemi(Just live well.) 2021-06-25 12:37:34 ...
-
6
在增删改查中的增和改操作中,我们经常需要更新数据流的创建人和修改人,无论我们项目是基于DDD,抑或是简单仅有服务层,此时我们都需要获取用户信息,那么我们只能将用户标识从控制器层层传递到服务或仓储层?鉴于如上场景很常见,难道我们没发觉这样做很是繁...
-
4
从选型到布置,一个合格的有声书工作角是这样的苹果产品等 3 个话题下的优秀答主作者:
-
3
《红楼梦》中薛宝钗为什么将自己的房间布置的雪洞一般? - 知乎43 个回答不然贾母说她
-
4
《有解·家居篇》 篇一:拯救强迫症 开关插座先布局 ...
-
6
V2EX › 程序员 出租屋内怎么布置一个高性价比的办公环境? x97bgt · 8 小时 0 分钟前 · 2...
-
8
← 木卫二的部分冰壳 可能是由”脆弱的冰“构成今日好价 0818 →
-
3
背景: 当前身份缓存对象顾名思义就是:当前登录的用户身份对象,那它解决了什么问题呢?其实在我们日常开发过程中经常能用的到几乎是必备的,就比如我给某个表插入数据时需要创建人或者一些权限的访问,都得用到当前身份缓存对象,当然啦今...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK