微信小程序仿摩拜单车

本小程序仿摩拜单车的地图显示和拖动部分,单车数据采用周边厕所模拟。index.wxml如下:

  1. <map id="map" bindcontroltap="bindcontroltap" bindregionchange="bindregionchange"

    longitude="{{jd}}" latitude="{{wd}}" markers="{{markers}}" controls="{{controls}}"

    style="width: 100%; height: {{height}}px;"  scale="16"   show-location>

  2. </map>

拖动地图,视野变化设置bindregionchange


页面加载时设置地图高度,control标注,头部广告及注册登录、返回当

前位置、优惠券、充值等。


  1. wx.getSystemInfo({

  2.      success: function (res) {

  3.        maph = res.windowHeight

  4.        mapw = res.windowWidth

  5.        that.setData({

  6.          height: maph,

  7.          controls: [

  8.            {

  9.              id: 1,

  10.              iconPath: '../libs/banner.jpg',

  11.              position: {

  12.                left: 10,

  13.                top: 10,

  14.                width: 390,

  15.                height: 80

  16.              },

  17.              clickable: true

  18.            },

  19.            {

  20.              id: 2,

  21.              iconPath: '../libs/gps.jpg',

  22.              position: {

  23.                left: 10,

  24.                top: maph - 50,

  25.                width: 40,

  26.                height: 40

  27.              },

  28.              clickable: true

  29.            },

  30.            {

  31.              id: 3,

  32.              iconPath: '../libs/log.jpg',

  33.              position: {

  34.                left: mapw / 2 - 80,

  35.                top: maph - 50,

  36.                width: 150,

  37.                height: 40

  38.              },

  39.              clickable: true

  40.            },

  41.            {

  42.              id: 4,

  43.              iconPath: '../libs/gift.jpg',

  44.              position: {

  45.                left: mapw - 60,

  46.                top: maph - 120,

  47.                width: 40,

  48.                height: 40

  49.              },

  50.              clickable: true

  51.            },

  52.            {

  53.              id: 5,

  54.              iconPath: '../libs/cost.jpg',

  55.              position: {

  56.                left: mapw - 60,

  57.                top: maph - 50,

  58.                width: 40,

  59.                height: 40

  60.              },

  61.              clickable: true

  62.            }

  63.          ]

  64.        })

由于需要频繁更新地图中心单车数据,对更新标记进行封装为getbike

  1. function getbike (){

  2.  console.log(mapcenter)

  3.  qqmapsdk.search({

  4.    location: {

  5.      latitude: mapcenter[0],

  6.      longitude: mapcenter[1]

  7.    },

  8.    keyword: '厕所',

  9.    success: function (res) {

  10.      var mark = []      

  11.      var getdata = res.data

  12.      for (var i = 0; i < getdata.length; i++) {

  13.        mark[i] = {

  14.          iconPath: "../libs/mobi.jpg",

  15.          id: i,

  16.          latitude: getdata[i].location.lat,

  17.          longitude: getdata[i].location.lng

  18.        }

  19.        mark.push({

  20.          iconPath: "../libs/c.png",

  21.          id: 99,

  22.          latitude: mapcenter[0],

  23.          longitude: mapcenter[1]

  24.        })

  25.      }

  26.      that.setData({

  27.        markers: mark

  28.      })

  29.    }

  30.  });

  31. }


页面初次加载,获取当前gps位置周围的单车数据

  1. onReady: function () {

  2.    var _this=this

  3.    wx.getLocation({

  4.      type: 'gcj02',

  5.      success: function (res) {

  6.        latitude1 = res.latitude

  7.        longitude1 = res.longitude

  8.        mapcenter = [latitude1, longitude1]

  9.        getbike()

  10.            _this.setData({

  11.              wd: latitude1,

  12.              jd: longitude1,

  13.            })

  14.        }

  15.    })

  16.  },



拖动地图时,先获取地图中心坐标,然后再调用getbike。

  1. //获取中心点

  2.  bindregionchange: function (e) {

  3.    this.mapCtx.getCenterLocation({

  4.      success: function (res) {

  5.        mapcenter = [res.latitude, res.longitude]

  6.      }

  7.    })

  8.    console.log(mapcenter)

  9.         getbike()

  10.  }

    ,

点击控件,根据控件id不同进行处理

  1. //控件

  2.  bindcontroltap: function (e){

  3. console.log(e.controlId)

  4.   var id=e.controlId

  5.   if (id==2){

  6.     this.mapCtx.moveToLocation()

  7.     mapcenter = [latitude1, longitude1]

  8.     getbike()

  9.   }

  10.   else if(id==1){

  11.     wx.navigateTo({

  12.       url: '../free30/free30'

  13.     })

  14.   }

  15.   else if (id==3) {

  16.     wx.navigateTo({

  17.       url: '../log/log'

  18.     })

  19.   }

  20.  }

  21. })


效果图如下:

微信小程序仿摩拜单车