在健康码中,主要的难点技术就是在小程序中定位、自定顶部导航。

自定义导航

在微信小程序中,默认的顶部导航不能设置图片背景或者是透明背景,只能自定义导航。

在每一个页面中引入,就是得到自己定义【个性化的】导航。

第一步:

现在app.json文件中配置

"window": {"navigationStyle": "custom"},

"navigationStyle": "custom"设置为使用自定义导航。

第三步:

开始定义导航

navbar.wxml:

<view class='nav-wrap'><!-- 导航栏背景图片 --><!-- <image class="backgroundimg" src="{{bg}}" bindload="imgLoaded"/> --><!-- // 导航栏 中间的标题 --><view class='nav-title'  style='line-height: {{height*2 + 44}}px; color:#fff'>{{navbarData.title}}</view>
</view>

navbar.js

const app = getApp()
Component({properties: {navbarData: {//navbarData   由父页面传递的数据,变量名字自命名type: Object,value: {},observer: function(newVal, oldVal) {}}},data: {height: '',//默认值  默认显示左上角navbarData: {showCapsule: 1},imageWidth: wx.getSystemInfoSync().windowWidth, // 背景图片的高度imageHeight: '', // 背景图片的长度,通过计算获取},attached: function() {// 获取是否是通过分享进入的小程序this.setData({share: app.globalData.share})// 定义导航栏的高度   方便对齐this.setData({height: app.globalData.height})},methods: {// 返回上一页面_navback() {wx.navigateBack()},// 计算图片高度imgLoaded(e) {console.log(e.detail.height)this.setData({imageHeight: e.detail.height *  (wx.getSystemInfoSync().windowWidth / e.detail.width)})},tapName: function () {console.log(0)}//返回到首页// _backhome() {//   wx.switchTab({//     url: '/pages/index/index'//   })// }},
})

在app.js中获取设备顶部窗口的高度,会根据这个值来设置自定义导航栏的高度

//app.js
App({onLaunch: function(e) {console.log(e)// 展示本地存储能力var logs = wx.getStorageSync('logs') || []logs.unshift(Date.now())wx.setStorageSync('logs', logs)// 登录wx.login({success: res => {// 发送 res.code 到后台换取 openId, sessionKey, unionId}})// 获取用户信息wx.getSetting({success: res => {if (res.authSetting['scope.userInfo']) {// 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框wx.getUserInfo({success: res => {// 可以将 res 发送给后台解码出 unionIdthis.globalData.userInfo = res.userInfo// 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回// 所以此处加入 callback 以防止这种情况if (this.userInfoReadyCallback) {this.userInfoReadyCallback(res)}}})}}})wx.getSystemInfo({success: res => {console.log(res)this.globalData.height = res.statusBarHeight}})},globalData: {userInfo: null,height: 0 // 顶部高度}
})

navbar.wxss

/* navbar.wxss *//* 顶部要固定定位   标题要居中   自定义按钮和标题要和右边微信原生的胶囊上下对齐 */.nav-wrap {position: fixed;width: 100%;top: 0;background-image: linear-gradient(#2f52bc, #9198e5, #d0d9f4);color: #000;z-index: 9999999;overflow: hidden;height: 400rpx;
}/* 背景图 */
.backgroundimg {position: absolute;z-index: -1;width: 100%;height: 100%;
}/* 标题要居中 */.nav-title {position: absolute;text-align: center;max-width: 400rpx;overflow: hidden;text-overflow: ellipsis;white-space: nowrap;top: 0;left: 0;right: 0;bottom: 0;margin: auto;font-size: 36rpx;color: #2c2b2b;font-weight: 450;
}.nav-capsule {display: flex;align-items: center;margin-left: 30rpx;width: 140rpx;justify-content: space-between;height: 100%;
}.back-pre {width: 32rpx;height: 36rpx;margin-top: 4rpx;padding: 10rpx;
}.nav-capsule {width: 36rpx;height: 40rpx;margin-top: 3rpx;
}

navbar.json:

{"component": true,"usingComponents": {}
}

在index中引入导航

index.json:

{"usingComponents": {"nav-bar": "../component/navbar/index"},"navigationBarTextStyle": "white"}

index.js:

Page({data:[nvabarData: {showCapsule: 1, //是否显示左上角图标   1表示显示    0表示不显示title: '健康码', //导航栏 中间的标题white: true, // 是就显示白的,不是就显示黑的。}]
})

index.wxml:

<nav-bar navbar-data='{{nvabarData}}'></nav-bar>

以上是首页引用的自定义导航,重新定义其他页面的导航:

wxml:

<view class='nav-wrap'><!-- 导航栏背景图片 --><!-- <image class="backgroundimg" src="{{bg}}" bindload="imgLoaded"/> --><!-- // 导航栏 中间的标题 --><view class='nav-title' wx:if='{{!navbarData.white}}' style='line-height: {{height*2 + 44}}px;'>{{navbarData.title}}</view><view class='nav-title' wx:else='{{!navbarData.white}}' style='line-height: {{height*2 + 44}}px; color:#fff'>{{navbarData.title}}</view><view style='display: flex; justify-content: space-around;flex-direction: column'><view class='nav-capsule' style='height: {{height*2 + 44}}px;'><view bindtap='_navback'><image src='{{backIcon}}' mode='aspectFit' class='back-pre'></image></view></view></view>
</view>

wxss:

/* navbar.wxss *//* 顶部要固定定位   标题要居中   自定义按钮和标题要和右边微信原生的胶囊上下对齐 */.nav-wrap {position: fixed;width: 100%;top: 0;background-image: linear-gradient(#2f52bc, #9198e5, #d0d9f4);color: #000;z-index: 9999999;overflow: hidden;}/* 背景图 */.backgroundimg {position: absolute;z-index: -1;width: 100%;height: 100%;
}/* 标题要居中 */.nav-title {position: absolute;text-align: center;max-width: 400rpx;overflow: hidden;text-overflow: ellipsis;white-space: nowrap;top: 0;left: 0;right: 0;bottom: 0;margin: auto;font-size: 36rpx;color: #2c2b2b;font-weight: 450;
}.nav-capsule {display: flex;align-items: center;margin-left: 30rpx;width: 140rpx;justify-content: space-between;height: 100%;
}.back-pre {width: 32rpx;height: 36rpx;margin-top: 4rpx;padding: 10rpx;
}.nav-capsule {width: 36rpx;height: 40rpx;margin-top: 3rpx;
}

json:

{"component": true,"usingComponents": {}
}

js:

const app = getApp()
Component({properties: {navbarData: {//navbarData   由父页面传递的数据,变量名字自命名type: Object,value: {},observer: function(newVal, oldVal) {}}},data: {height: '',//默认值  默认显示左上角navbarData: {showCapsule: 1},imageWidth: wx.getSystemInfoSync().windowWidth, // 背景图片的高度imageHeight: '', // 背景图片的长度,通过计算获取backIcon: "../../img/fanhui.png",},attached: function() {// 获取是否是通过分享进入的小程序this.setData({share: app.globalData.share})// 定义导航栏的高度   方便对齐this.setData({height: app.globalData.height})},methods: {// 返回上一页面_navback() {wx.navigateBack()},// 计算图片高度imgLoaded(e) {console.log(e.detail.height)this.setData({imageHeight: e.detail.height *  (wx.getSystemInfoSync().windowWidth / e.detail.width)})},tapName: function() {console.log(0)}//返回到首页// _backhome() {//   wx.switchTab({//     url: '/pages/index/index'//   })// }},
})

其实可以不用定义两个导航,只要navbar.js判断是否是通过分享进入小程序或者是当前页面是否是首页,来决定隐藏/显示导航左上角的返回按钮。

地图定位

在监听小程序初次渲染完成时,通过apiwx.getLocation获取小程序当前的经纬度,再通过经纬度在地图给小程序进行定位。但是这个API需要用户授权后才能获取到,只需要在首次打开小程序时 进行授权即可。

在app.json设置:

"permission": {"scope.userLocation": {"desc": "你的位置信息将用于小程序位置接口的效果展示"}}


授权过后,在首页的index.js中的onReady监听页面渲染函数中获取小程序的经纬度:

  onReady: function() {const that = this;const markers = that.data.markers;wx.getLocation({type: 'wgs84',success(res) {markers.latitude = res.latitude;markers.longitude = res.longitude;that.setData({latitude: res.latitude,longitude: res.longitude,markers: markers})const latitude = res.latitudeconst longitude = res.longitude}})},

index.js完整代码如下:

//index.js
//获取应用实例
const app = getApp()
Page({data: {markers: [{  //地图标注的信息iconPath: "../resources/location.png",id: 0,latitude: 26.64702,longitude: 106.63024,width: 50,height: 50}],latitude: 26.64702,longitude: 106.63024,nvabarData: {showCapsule: 1, //是否显示左上角图标   1表示显示    0表示不显示title: '健康码', //导航栏 中间的标题white: true, // 是就显示白的,不是就显示黑的。},// 导航头的高度height: app.globalData.height * 2 + 20,userImg: "../img/user1.png",cradImg: "../img/user2.png",clickCradimg: "../img/jilu.png",rightimg: "../img/rightimg.png"},onLoad: function() {},onReady: function() {const that = this;const markers = that.data.markers;wx.getLocation({type: 'wgs84',success(res) {console.log(res)markers.latitude = res.latitude;markers.longitude = res.longitude;that.setData({latitude: res.latitude,longitude: res.longitude,markers: markers})const latitude = res.latitudeconst longitude = res.longitude}})},login: function() {wx.navigateTo({url: '/pages/userinfo/index',})},card: function() {wx.navigateTo({url: '/pages/card/index',})},punchCard: function() {wx.navigateTo({url: '/pages/punchcard/index',})}
})

index.wxml

<nav-bar navbar-data='{{nvabarData}}'></nav-bar>
<map id="map" longitude="{{longitude}}" latitude="{{latitude}}" scale="14" bindcontroltap="controltap" markers="{{markers}}"  show-location>
</map><view class="info"><view class="login" bindtap="login"><image src="{{userImg}}"></image><view>信息登录</view></view><view class="crad" bindtap="card"><image src="{{cradImg}}"></image><view>健康卡</view></view>
</view>
<view class="clickCrad" bindtap="punchCard"><view class="left"><image class="cradImg" src="{{clickCradimg}}"></image><view>每日健康打卡</view></view><image src="{{rightimg}}" class="rightImg"></image>
</view>

index.wxss:

#map {width: 100%;height: 100%;
}.info {position: fixed;top: 250rpx;padding: 30rpx 100rpx;background: #fff;border-radius: 10px;display: flex;justify-content: space-between;font-weight: bold;z-index: 99999999999;left: 0;right: 0;margin: 0 40rpx;
}.info image {width: 100rpx;height: 100rpx;
}.login {text-align: center;
}.crad {text-align: center;
}.clickCrad {background: #fff;margin: 10rpx 40rpx 20rpx 40rpx;padding: 20rpx 100rpx;border-radius: 10px;display: flex;justify-content: space-between;align-items: center;position: fixed;top: 500rpx;right: 0;left: 0;z-index: 999999999999;
}.left {display: flex;align-items: center;
}.cradImg {width: 50rpx;height: 50rpx;margin-right: 10px;
}.rightImg {width: 35rpx;height: 35rpx;
}

微信小程序中自定义导航和地图定位相关推荐

  1. 微信小程序的自定义导航栏

    微信小程序的导航栏主要分为两个部分:状态栏和标题栏. 关于微信小程序的自定义导航栏也就是通过微信小程序官方提供的API接口来算出状态栏和标题栏的高度. 注意获取到的数值单位都是px. 首先要开启自定义 ...

  2. 微信小程序,自定义导航栏组件

    微信小程序,自定义导航栏组件,可兼容iPhone 11及以上留海屏显示,关于参数获取设置参照微信小程序-收藏_羽筠的博客-CSDN博客 可定义设置的内容如下: 文字及返回箭头颜色 背景图片(优先级高于 ...

  3. 微信小程序中自定义背景导航栏透明背景设置

    首先确定好自己要在哪个页面中使用自定义头部导航栏样式 在该页面中找到xxx.json文件中添加上 "navigationStyle":''custom" 这样在页面中头部 ...

  4. 微信小程序可以加服务器上的字体,微信小程序中自定义字体

    微信小程序支持自定义字体开放出来也有段时间,这边整理下使用自定义字体中,容易忽略的一些问题,和简便的全局自定义方式.如果是同时加载两种字体包,先下载下来的会被后下载下来的字体包给覆盖. 官网接口文档 ...

  5. 微信小程序中自定义组件

    文章目录 小程序项目 app.json pages/index/index.wxml pages/index/index.wxss pages/index/index.js 自定义组件 compone ...

  6. 微信小程序 ios自定义导航栏 下拉“橡皮筋” 效果

    问题: 1.微信小程序ios中如果自定义导航栏取消了橡皮筋效果页面无法滑动 2.如果不取消,页面往下拉时会与顶部有一大片空白(安卓本身scrollTop不会有负值) 3.fixed之后absolute ...

  7. 微信小程序实现自定义导航菜单搜索栏

    先附上效果图,要达到的效果就是如图所示,把原型的导航菜单去掉,换成对应的搜索栏. 其实还是做法还是比较简单,首先讲一下实现的原理(原理明白实现起来就容易了): 1.先隐藏微信小程序自带的导航菜单:   ...

  8. 如何在微信小程序中调用腾讯地图api

    微信小程序的地图api是非常有限的,如果要搜索地图上的位置,比如附近的医院.学校等,就需要使用地图api,使用腾讯地图api的过程如下: 一.开发者申请腾讯地图 进入官网http://lbs.qq.c ...

  9. 微信小程序中顶部导航栏全局切换

    一.场景 微信小程序和企业微信小程序共用一个,从不同入口进入小程序显示不同的顶部导航栏 二.思路 1.app.json中全局设置只能写死一个,无法实现 2.app.js中直接使用wx.setNavig ...

最新文章

  1. python程序采用unicode编码、英文字符,Python 与 Unicode
  2. php下dat函数e,为你总结一些php信息函数
  3. 094、Swarm 中最重要的概念(Swarm01)
  4. win10 64 安装VSS2005报错,解决方法。
  5. 微前端在网易七鱼的实践
  6. 使用docker-compose搭建AspNetCore开发环境
  7. SSH集成log4j日志环境
  8. 【Es】Es 集群设置分片很大导致集群无法选举主节点异常等
  9. linux给命令起别名命令,alias命令 – 设置命令别名
  10. JVM常量池和八种基本数据及字符串
  11. 极客大学架构师训练营 数据结构与算法 平衡二叉树 红黑树 动态规划 遗传算法 第15课 听课总结
  12. 【软考】中级软件设计师的一些知识点笔记(22.2.10)
  13. liinux下安装jdk
  14. [Introduction to programming in Java 笔记] 1.3.8 Gambler's ruin simulation 赌徒破产模拟
  15. 小米总监说软件测试分为这及类
  16. Android手机投屏win10
  17. 一键百度 一键翻译 云脉CC慧眼百度搜索版
  18. 学校网站建设要做好四个难点
  19. python运维工程师面试题_新浪软件测试面试题-Linux运维工程师面试真题
  20. LeetCode刷题笔记-逃脱阻碍者

热门文章

  1. 雅思口语有技巧,陈家桥雅思培训有妙招
  2. 【MySQL教程(一)】安装和初步使用
  3. 超级天碟《竹宴》(320K)
  4. 概率随机问题【2】 取样与概率
  5. 红米note5解锁教程_红米NOTE5手机忘密码了怎么办?修手机的师傅教给我一个简单方法,解锁其实很简单...
  6. ssm爱尚购物毕业设计-附源码211622
  7. What are DASD volumes and labels?
  8. win to go 给移动硬盘装双系统
  9. scikit learn各个常用模型调参总结
  10. 视频存储与公众号中播放的解决方案(直接调用代码或公众号调用视频地址)