微信小程序实现按首字母检索城市列表

前端之家收集整理的这篇文章主要介绍了微信小程序实现按首字母检索城市列表前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

不说废话,上效果

因为我有多处要用到,所以我这里是写成自定义组件的,你也可以直接改成在page页面编写:

布局左边一个scroll-view,显示城市列表,右边一个view显示字母列表,城市列表这边有首字母显示,给这个添加这个字母的ID,然后给右边的26个字母添加点击事件,点击的时候获取到点击的是哪个字母,给scroll-view的scroll-into-view赋值相应的字母,它左边就可以跳到相应的地方,再给scroll-view 加一个scroll-with-animation,让它跳转的时候有动画效果

首先,我们来看看wxml

  1.  

  1. Box{
  2. height:100%;
  3. background: #fff;
  4. display: flex;
  5. }
  6. .city_left{
  7. flex: 1;
  8. }
  9. .city_right{
  10. width: 60rpx;
  11. display: flex;
  12. flex-direction: column;
  13. justify-content: space-around;
  14. }
  15. .letter_item{
  16. flex: 1;
  17. display: block;
  18. font-size: 24rpx;
  19. color: #33B9FF;
  20. text-align: center;
  21. }
  22. .city_locate,.national{
  23. height: 80rpx;
  24. line-height: 80rpx;
  25. border-bottom: 1px solid #efefef;
  26. font-size: 28rpx;
  27. color: #333;
  28. padding-left: 25rpx;
  29. }
  30. .city_locate_title{
  31. color: #999;
  32. margin-right: 20rpx;
  33. }
  34. .new_city{
  35. background: #efefef;
  36. font-size: 28rpx;
  37. }
  38. .new_city_title{
  39. line-height: 50rpx;
  40. color: #999;
  41. padding-left: 25rpx;
  42. margin-bottom: 20rpx;
  43. }
  44. .new_city_Box{
  45. display: flex;
  46. flex-wrap: wrap;
  47. }
  48. .new_city_text{
  49. width: 200rpx;
  50. text-align: center;
  51. line-height: 70rpx;
  52. background: #fff;
  53. border-radius: 35rpx;
  54. margin:0 0 22rpx 22rpx;
  55. }
  56. .city_first_letter{
  57. line-height: 40rpx;
  58. height: 40rpx;
  59. padding-left: 25rpx;
  60. font-size: 28rpx;
  61. background: #eee;
  62. color: #999;
  63. }
  64. .city_name{
  65. display: block;
  66. line-height: 80rpx;
  67. height: 80rpx;
  68. border-bottom: 1px solid #efefef;
  69. font-size: 28rpx;
  70. color: #333;
  71. padding-left: 25rpx;
  72. }

文件,因为我这里是组件,所以是下面这样,如果你不是的组件,那么不要这句

  1.  

     1800000)){
  2. {

    微信小程序sdk获取当前经纬度的详情信息,然后取到当前城市,这是腾讯地图微信小程序JavaScript SDK,可以去查看教程,这里用到的是地址解析功能

    获取定位信息getLocateInfo(){
  3. let that=this;return new Promise(function (resolve, reject) {
  4. that.location().then(function(val){//如果通过授权,那么直接使用腾讯的微信小程序sdk获取当前定位城市qqwxmap.reverseGeocoder({
  5. location: {
  6. latitude: val.latitude,
  7. longitude: val.longitude
  8. },
  9. success: function (res) {
  10. console.log(res.result.address_component.city);
  11. resolve(res.result.address_component.city);//返回城市},
  12. fail: function (res) {
  13. reject(res);
  14. },
  15. complete: function (res) {
  16. console.log(res);
  17. }
  18. });
  19. },function(error) {//如果用户拒绝了授权,那么这里会提醒他,去授权后再定位
  20. console.log('shibai');
  21. wx.showModal({
  22. title: '',
  23. content: '自动定位需要授权地理定位选项',
  24. confirmText: '去授权',
  25. success(res) {if (res.confirm) {
  26. wx.openSetting({
  27. success(res) {
  28. console.log(res);
  29. that.getLocateInfo();
  30. }
  31. })
  32. }
  33. }
  34. })
  35. })
  36. })
  37. }
  38. //定位,获取当前经纬度location(){ 
  39. return new Promise(function (resolve, reject) {
  40. wx.getLocation({
  41. altitude: true,
  42. success: function (res) {
  43. resolve(res);
  44. },fail(res){
  45. reject(res);
  46. }
  47. })
  48. });
  49. }
  50. }

    页面的json文件里要添加这一句

    然后在引用的wxml界面添加组件,styles是设置的组件的变量,我这里是可以改变组件最外层的样式,bindcitytap是上面组件js里的点击城市方法里提到的事件

    然后在引用的界面的js里,写个cityTap事件,获取传过来的值

     页面里面,需要改一下

    首先wxml里面去掉圈起来的这句

     添加

     1800000)) {//每隔30分钟请求一次定位
  51. this.getLocate();
  52. } else {//如果未满30分钟,那么直接从本地缓存里取值that.setData({
  53. locateCity: cityOrTime.city
  54. })
  55. }
  56. }
  57. })

    微信小程序实现按首字母检索城市列表的代码全部内容,希望对你开发微信小程序有所帮助。

猜你在找的微信小程序相关文章