“案例: 运动打卡:跑步定位”的版本间的差异

来自CloudWiki
跳转至: 导航搜索
(创建页面,内容为“# wsx ``` var filter ={ toFix:function(value){ return value.toFixed(2) // 保留2位小数 } } module.exports={ toFix: filter.toFix } ``` # js ``` var…”)
 
 
第1行: 第1行:
# wsx
+
<h1>wsx</h1>
 
+
<pre><code>var filter ={
```
 
var filter ={
 
 
   toFix:function(value){
 
   toFix:function(value){
 
     return value.toFixed(2) // 保留2位小数
 
     return value.toFixed(2) // 保留2位小数
第11行: 第9行:
 
   toFix: filter.toFix
 
   toFix: filter.toFix
 
}
 
}
```
+
</code></pre>
 
+
<h1>js</h1>
# js
+
<pre><code>var QQMapWX = require(&#39;../../libs/qqmap-wx-jssdk.js&#39;);
 
 
```
 
var QQMapWX = require('../../libs/qqmap-wx-jssdk.js');
 
 
var qqmapsdk = new QQMapWX({
 
var qqmapsdk = new QQMapWX({
   key: 'IWLBZ-CK6R4-YXDUO-DQCXK-J5H4Z-DSBCI'
+
   key: &#39;IWLBZ-CK6R4-YXDUO-DQCXK-J5H4Z-DSBCI&#39;
 
});
 
});
 
var app = getApp();
 
var app = getApp();
第37行: 第32行:
 
     time: app.globalData.time,
 
     time: app.globalData.time,
 
     markers: [{
 
     markers: [{
       iconPath: "../../imgs/pic.jpg",
+
       iconPath: &quot;../../imgs/pic.jpg&quot;,
 
       id: 0,
 
       id: 0,
 
       latitude: 39.9205,
 
       latitude: 39.9205,
第50行: 第45行:
 
     var that = this
 
     var that = this
 
     wx.getLocation({
 
     wx.getLocation({
       type: 'gcj02',
+
       type: &#39;gcj02&#39;,
 
       success(res) {
 
       success(res) {
 
         that.setData({
 
         that.setData({
第71行: 第66行:
 
           target_lo: res.longitude,
 
           target_lo: res.longitude,
 
           markers: [{
 
           markers: [{
             iconPath: "../../imgs/pic.jpg",
+
             iconPath: &quot;../../imgs/pic.jpg&quot;,
 
             id: 0,
 
             id: 0,
 
             latitude: 39.9205,
 
             latitude: 39.9205,
第78行: 第73行:
 
             height: 30
 
             height: 30
 
           }, {
 
           }, {
             iconPath: "../../imgs/blue.png",
+
             iconPath: &quot;../../imgs/blue.png&quot;,
 
             id: 1,
 
             id: 1,
 
             latitude: res.latitude,
 
             latitude: res.latitude,
第91行: 第86行:
 
   routing() {
 
   routing() {
 
     var that = this
 
     var that = this
     var temp_loc = this.data.target_la + ',' + this.data.target_lo
+
     var temp_loc = this.data.target_la + &#39;,&#39; + this.data.target_lo
 
     qqmapsdk.direction({
 
     qqmapsdk.direction({
       mode: 'walking',
+
       mode: &#39;walking&#39;,
 
       to: temp_loc,
 
       to: temp_loc,
 
       success: function (res) {
 
       success: function (res) {
第101行: 第96行:
 
         //坐标解压(返回的点串坐标,通过前向差分进行压缩)
 
         //坐标解压(返回的点串坐标,通过前向差分进行压缩)
 
         var kr = 1000000;
 
         var kr = 1000000;
         for (var i = 2; i < coors.length; i++) {
+
         for (var i = 2; i &lt; coors.length; i++) {
 
           coors[i] = Number(coors[i - 2]) + Number(coors[i]) / kr;
 
           coors[i] = Number(coors[i - 2]) + Number(coors[i]) / kr;
 
         }
 
         }
 
         //将解压后的坐标放入点串数组pl中
 
         //将解压后的坐标放入点串数组pl中
         for (var i = 0; i < coors.length; i += 2) {
+
         for (var i = 0; i &lt; coors.length; i += 2) {
 
           pl.push({ latitude: coors[i], longitude: coors[i + 1] })
 
           pl.push({ latitude: coors[i], longitude: coors[i + 1] })
 
         }
 
         }
第115行: 第110行:
 
           polyline: [{
 
           polyline: [{
 
             points: pl,
 
             points: pl,
             color: '#FF0000DD',
+
             color: &#39;#FF0000DD&#39;,
 
             width: 4
 
             width: 4
 
           }]
 
           }]
第127行: 第122行:
 
     app.globalData.begin_la = this.data.latitude
 
     app.globalData.begin_la = this.data.latitude
 
     app.globalData.begin_lo = this.data.longitude
 
     app.globalData.begin_lo = this.data.longitude
     app.globalData.interval = setInterval(() => {
+
     app.globalData.interval = setInterval(() =&gt; {
 
       this.running()
 
       this.running()
 
       this.runPath()
 
       this.runPath()
第140行: 第135行:
 
       time: app.globalData.time,
 
       time: app.globalData.time,
 
     })
 
     })
     app.globalData.interval = setInterval(() => {
+
     app.globalData.interval = setInterval(() =&gt; {
 
       this.running()
 
       this.running()
 
       this.runPath()
 
       this.runPath()
第148行: 第143行:
 
     clearInterval(app.globalData.interval)
 
     clearInterval(app.globalData.interval)
 
     this.geoInfo()
 
     this.geoInfo()
     console.log('开始位置', app.globalData.begin_la.begin_la, app.globalData.begin_la.begin_lo)
+
     console.log(&#39;开始位置&#39;, app.globalData.begin_la.begin_la, app.globalData.begin_la.begin_lo)
     console.log('结束位置', this.data.latitude, this.data.longitude)
+
     console.log(&#39;结束位置&#39;, this.data.latitude, this.data.longitude)
 
   },
 
   },
 
   clearRun(){
 
   clearRun(){
     console.log('数据已清除')
+
     console.log(&#39;数据已清除&#39;)
 
     app.globalData={}
 
     app.globalData={}
 
   },
 
   },
第173行: 第168行:
 
       polyline: [{
 
       polyline: [{
 
         points: app.globalData.path,
 
         points: app.globalData.path,
         color: '#FF0000DD',
+
         color: &#39;#FF0000DD&#39;,
 
         width: 4
 
         width: 4
 
       }],
 
       }],
 
       markers: [{
 
       markers: [{
         iconPath: "../../imgs/triangle.png",
+
         iconPath: &quot;../../imgs/triangle.png&quot;,
 
         id: 3,
 
         id: 3,
 
         latitude: app.globalData.begin_la,
 
         latitude: app.globalData.begin_la,
第184行: 第179行:
 
         height: 20
 
         height: 20
 
       }, {
 
       }, {
         iconPath: "../../imgs/yellow.png",
+
         iconPath: &quot;../../imgs/yellow.png&quot;,
 
         id: 4,
 
         id: 4,
 
         latitude: this.data.latitude,
 
         latitude: this.data.latitude,
第196行: 第191行:
 
     var that = this
 
     var that = this
 
     wx.getLocation({
 
     wx.getLocation({
       type: 'gcj02',
+
       type: &#39;gcj02&#39;,
 
       success(res) {
 
       success(res) {
 
         that.setData({
 
         that.setData({
第212行: 第207行:
 
   }
 
   }
 
})
 
})
```
+
</code></pre>
 
+
<h1>json</h1>
# json
+
<pre><code>{
 
+
   &quot;usingComponents&quot;: {}
```
 
{
 
   "usingComponents": {}
 
 
}
 
}
```
+
</code></pre>
 
+
<h1>wxml</h1>
# wxml
+
<pre><code>&lt;wxs module=&#39;filter&#39; src=&#39;fix.wxs&#39;&gt;&lt;/wxs&gt;
 
 
```
 
<wxs module='filter' src='fix.wxs'></wxs>
 
 
 
<view class='mainView'>
 
  <view>路线规划</view>
 
  <view class='top-buttons'>
 
    <button class='top-btn' bindtap='pickLocation'>选择位置</button>
 
    <button class='top-btn' bindtap='routing'>显示路线</button>
 
  </view>
 
 
 
  <map style="width:100%; height:350px" longitude='{{longitude}}' latitude='{{latitude}}' markers='{{markers}}' polyline="{{polyline}}">
 
    <cover-view class="cover">
 
      <cover-view>跑步距离:{{filter.toFix(meters)}} </cover-view>
 
      <cover-view>跑步时长:{{filter.toFix(time)}}</cover-view>
 
    </cover-view>
 
  </map>
 
  
   <view>跑步定位</view>
+
&lt;view class=&#39;mainView&#39;&gt;
   <view class='bottom-buttons'>
+
   &lt;view&gt;路线规划&lt;/view&gt;
    <button class='bottom-btn' bindtap='startRun'>跑步开始</button>
+
   &lt;view class=&#39;top-buttons&#39;&gt;
     <button class="bottom-btn" bindtap="continueRun">继续跑步</button>
+
     &lt;button class=&#39;top-btn&#39; bindtap=&#39;pickLocation&#39;&gt;选择位置&lt;/button&gt;
     <button class="bottom-btn" bindtap="stopRun">结束跑步</button>
+
     &lt;button class=&#39;top-btn&#39; bindtap=&#39;routing&#39;&gt;显示路线&lt;/button&gt;
    <button class="bottom-btn" bindtap="clearRun">清除路线</button>
+
   &lt;/view&gt;
   </view>
 
</view>
 
```
 
  
# wxss
+
  &lt;map style=&quot;width:100%; height:350px&quot; longitude=&#39;{{longitude}}&#39; latitude=&#39;{{latitude}}&#39; markers=&#39;{{markers}}&#39; polyline=&quot;{{polyline}}&quot;&gt;
 +
    &lt;cover-view class=&quot;cover&quot;&gt;
 +
      &lt;cover-view&gt;跑步距离:{{filter.toFix(meters)}} &lt;/cover-view&gt;
 +
      &lt;cover-view&gt;跑步时长:{{filter.toFix(time)}}&lt;/cover-view&gt;
 +
    &lt;/cover-view&gt;
 +
  &lt;/map&gt;
  
```
+
  &lt;view&gt;跑步定位&lt;/view&gt;
.mainView {
+
  &lt;view class=&#39;bottom-buttons&#39;&gt;
 +
    &lt;button class=&#39;bottom-btn&#39; bindtap=&#39;startRun&#39;&gt;跑步开始&lt;/button&gt;
 +
    &lt;button class=&quot;bottom-btn&quot; bindtap=&quot;continueRun&quot;&gt;继续跑步&lt;/button&gt;
 +
    &lt;button class=&quot;bottom-btn&quot; bindtap=&quot;stopRun&quot;&gt;结束跑步&lt;/button&gt;
 +
    &lt;button class=&quot;bottom-btn&quot; bindtap=&quot;clearRun&quot;&gt;清除路线&lt;/button&gt;
 +
  &lt;/view&gt;
 +
&lt;/view&gt;
 +
</code></pre>
 +
<h1>wxss</h1>
 +
<pre><code>.mainView {
 
   background-color: #5eb2df;
 
   background-color: #5eb2df;
 
   color: #fff;
 
   color: #fff;
第292行: 第278行:
 
}
 
}
  
```
+
</code></pre>
 +
<p>&nbsp;</p>

2020年5月20日 (三) 08:40的最新版本

wsx

<code>var filter ={
  toFix:function(value){
    return value.toFixed(2) // 保留2位小数
  }
}

module.exports={
  toFix: filter.toFix
}
</code>

js

<code>var QQMapWX = require('../../libs/qqmap-wx-jssdk.js');
var qqmapsdk = new QQMapWX({
  key: 'IWLBZ-CK6R4-YXDUO-DQCXK-J5H4Z-DSBCI'
});
var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    latitude: 0,
    longitude: 0,
    target_la: 0,
    target_lo: 0,
    speed: 0,
    accuracy: 0,
    meters: app.globalData.meters,
    time: app.globalData.time,
    markers: [{
      iconPath: "../../imgs/pic.jpg",
      id: 0,
      latitude: 39.9205,
      longitude: 116.4605,
      width: 30,
      height: 30
    }],
    path: [],

  },
  geoInfo() {
    var that = this
    wx.getLocation({
      type: 'gcj02',
      success(res) {
        that.setData({
          latitude: res.latitude,
          longitude: res.longitude,
          speed: res.speed,
          accuracy: res.accuracy,
        })
      }
    })
    console.log(this.data.latitude, this.data.longitude)
  },
  pickLocation() {
    var that = this
    wx.chooseLocation({
      success: function (res) {
        console.log(res)
        that.setData({
          target_la: res.latitude,
          target_lo: res.longitude,
          markers: [{
            iconPath: "../../imgs/pic.jpg",
            id: 0,
            latitude: 39.9205,
            longitude: 116.4605,
            width: 30,
            height: 30
          }, {
            iconPath: "../../imgs/blue.png",
            id: 1,
            latitude: res.latitude,
            longitude: res.longitude,
            width: 20,
            height: 20
          }]
        })
      },
    })
  },
  routing() {
    var that = this
    var temp_loc = this.data.target_la + ',' + this.data.target_lo
    qqmapsdk.direction({
      mode: 'walking',
      to: temp_loc,
      success: function (res) {
        console.log(res)
        var ret = res;
        var coors = ret.result.routes[0].polyline, pl = [];
        //坐标解压(返回的点串坐标,通过前向差分进行压缩)
        var kr = 1000000;
        for (var i = 2; i < coors.length; i++) {
          coors[i] = Number(coors[i - 2]) + Number(coors[i]) / kr;
        }
        //将解压后的坐标放入点串数组pl中
        for (var i = 0; i < coors.length; i += 2) {
          pl.push({ latitude: coors[i], longitude: coors[i + 1] })
        }
        console.log(pl)
        //设置polyline属性,将路线显示出来,将解压坐标第一个数据作为起点
        that.setData({
          latitude: pl[0].latitude,
          longitude: pl[0].longitude,
          polyline: [{
            points: pl,
            color: '#FF0000DD',
            width: 4
          }]
        })
      }
    })
  },
  startRun() {
    clearInterval(app.globalData.interval)
    this.geoInfo()
    app.globalData.begin_la = this.data.latitude
    app.globalData.begin_lo = this.data.longitude
    app.globalData.interval = setInterval(() => {
      this.running()
      this.runPath()
    }, 1000)
  },
  continueRun(){
    clearInterval(app.globalData.interval)
    this.setData({
      latitude: app.globalData.latitude,
      longitude: app.globalData.longitude,
      meters: app.globalData.meters,
      time: app.globalData.time,
    })
    app.globalData.interval = setInterval(() => {
      this.running()
      this.runPath()
    }, 1000)
  },
  stopRun() {
    clearInterval(app.globalData.interval)
    this.geoInfo()
    console.log('开始位置', app.globalData.begin_la.begin_la, app.globalData.begin_la.begin_lo)
    console.log('结束位置', this.data.latitude, this.data.longitude)
  },
  clearRun(){
    console.log('数据已清除')
    app.globalData={}
  },
  running() {
    this.setData({
      latitude: this.data.latitude + 0.0002,
      longitude: this.data.longitude + 0.0002,
      meters: this.data.meters + 0.05,
      time: this.data.time + 1,
    })
    app.globalData.latitude = this.data.latitude
    app.globalData.longitude = this.data.longitude
    app.globalData.meters = this.data.meters
    app.globalData.time = this.data.time
    console.log(this.data)
  },
  runPath() {
    app.globalData.path.push({ latitude: this.data.latitude, longitude: this.data.longitude })
    this.setData({
      polyline: [{
        points: app.globalData.path,
        color: '#FF0000DD',
        width: 4
      }],
      markers: [{
        iconPath: "../../imgs/triangle.png",
        id: 3,
        latitude: app.globalData.begin_la,
        longitude: app.globalData.begin_lo,
        width: 20,
        height: 20
      }, {
        iconPath: "../../imgs/yellow.png",
        id: 4,
        latitude: this.data.latitude,
        longitude: this.data.longitude,
        width: 20,
        height: 20
      }]
    })
  },
  onReady: function () {
    var that = this
    wx.getLocation({
      type: 'gcj02',
      success(res) {
        that.setData({
          latitude: res.latitude,
          longitude: res.longitude,
          speed: res.speed,
          accuracy: res.accuracy,
        })

      }
    })
  },
  onLoad: function () {

  }
})
</code>

json

<code>{
  "usingComponents": {}
}
</code>

wxml

<code><wxs module='filter' src='fix.wxs'></wxs>

<view class='mainView'>
  <view>路线规划</view>
  <view class='top-buttons'>
    <button class='top-btn' bindtap='pickLocation'>选择位置</button>
    <button class='top-btn' bindtap='routing'>显示路线</button>
  </view>

  <map style="width:100%; height:350px" longitude='{{longitude}}' latitude='{{latitude}}' markers='{{markers}}' polyline="{{polyline}}">
    <cover-view class="cover">
      <cover-view>跑步距离:{{filter.toFix(meters)}} </cover-view>
      <cover-view>跑步时长:{{filter.toFix(time)}}</cover-view>
    </cover-view>
  </map>

  <view>跑步定位</view>
  <view class='bottom-buttons'>
    <button class='bottom-btn' bindtap='startRun'>跑步开始</button>
    <button class="bottom-btn" bindtap="continueRun">继续跑步</button>
    <button class="bottom-btn" bindtap="stopRun">结束跑步</button>
    <button class="bottom-btn" bindtap="clearRun">清除路线</button>
  </view>
</view>
</code>

wxss

<code>.mainView {
  background-color: #5eb2df;
  color: #fff;
  text-align: center;
}

.top-buttons {
  display: flex;
  flex-direction: row;
}

.top-btn {
  margin-top: 10rpx;
  width: 300rpx;
  margin-bottom: 10rpx;
  font-size: 30rpx;
  align-items: center;
}

.cover{
  color: blue;
  text-align: left;  
}

.bottom-buttons {
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
}

.bottom-btn {
  margin-top: 10rpx;
  width: 300rpx;
  margin-bottom: 10rpx;
  font-size: 30rpx;
  align-items: center;
}

</code>