我已經(jīng)把全部代碼放在github上-weChatApp-Run,可以下載來(lái)看看或者先star收藏,我以后還會(huì)進(jìn)行一些優(yōu)化更新?,F(xiàn)在只是一個(gè)學(xué)習(xí)Demo,大家溝通學(xué)習(xí),實(shí)際應(yīng)用還需更多優(yōu)化。
1、注冊(cè)一個(gè)小程序賬號(hào),得用一個(gè)沒(méi)注冊(cè)過(guò)公眾號(hào)的郵箱注冊(cè)。
2、注冊(cè)過(guò)程中需要很多認(rèn)證,有很多認(rèn)證,比較繁瑣,如果暫時(shí)只是開發(fā)測(cè)試,不進(jìn)行提審、發(fā)布的話,只要完成營(yíng)業(yè)執(zhí)照號(hào)填寫就可以了,不需要完成微信認(rèn)證。
3、注冊(cè)完賬號(hào),登錄,在主頁(yè)面左邊列表中點(diǎn)擊設(shè)置,然后再設(shè)置頁(yè)面中選開發(fā)設(shè)置就可以看到AppID,用于登錄開發(fā)工具。


可以到官網(wǎng)下載開發(fā)工具下載


打開開發(fā)者工具,選擇小程序選項(xiàng),到達(dá)添加項(xiàng)目頁(yè)面

這個(gè)時(shí)候在前面設(shè)置頁(yè)面的AppId就用到了。
如果項(xiàng)目目錄中的文件是個(gè)空文件夾,會(huì)提示是否創(chuàng)建quick start 項(xiàng)目。
選擇“是”,開發(fā)者工具會(huì)幫助我們?cè)陂_發(fā)目錄里生成一個(gè)簡(jiǎn)單的 demo。
這個(gè)Demo擁有一個(gè)完整的小程序的大概框架。
先看下一目錄:

app.js: 小程序邏輯,生命周期,,全局變量
app.json: 小程序公共設(shè)置,導(dǎo)航欄顏色等,不可以注釋
app.wxss :小程序公共樣式,類CSS 。

每一個(gè)小程序頁(yè)面是由同路徑下同名的四個(gè)不同后綴文件的組成,如:index.js、index.wxml、index.wxss、index.json。

微信小程序中的每一個(gè)頁(yè)面的【路徑+頁(yè)面名】都需要寫在 app.json 的 pages 中,且 pages 中的第一個(gè)頁(yè)面是小程序的首頁(yè)。

這四個(gè)文件按照功能可以分成三個(gè)部分:
配置:json 文件
邏輯層:js文件
視圖層:wxss.wxml文件
在 iOS 上,小程序的 javascript 代碼是運(yùn)行在 JavaScriptCore 中
在 Android 上,小程序的 javascript 代碼是通過(guò) X5 內(nèi)核來(lái)解析
在 開發(fā)工具上, 小程序的 javascript 代碼是運(yùn)行在 nwjs(chrome內(nèi)核) 中。所以開發(fā)工具上的效果跟實(shí)際效果有所出入。
微信提供了許多組件,主要分為八種:
視圖容器、
基礎(chǔ)內(nèi)容、
表單組件、
操作反饋、
導(dǎo)航、
媒體組件、
地圖、
畫布
包含view、scroll-view、button、form等普通常用的組件,也提供了地圖map、畫布canvas。
組件主要屬于視圖層,通過(guò)wxml來(lái)進(jìn)行結(jié)構(gòu)布局,類似于html。通過(guò)wxss修改樣式,類似于css。
組件使用語(yǔ)法實(shí)例:
<!--普通視圖--> <view>這是一個(gè)普通視圖</view> <!--wxss樣式修改--> <view clas="mainView">樣式修改過(guò)的視圖</view>
更多的組件以及相關(guān)使用方法可以到官方文檔-組件查看
網(wǎng)絡(luò)
媒體
數(shù)據(jù)
位置
設(shè)備
界面
開發(fā)接口
其中網(wǎng)絡(luò)請(qǐng)求的使用必須先到公眾平臺(tái)登錄小程序賬號(hào),在設(shè)置頁(yè)面那里,設(shè)置允許訪問(wèn)的域名,網(wǎng)絡(luò)請(qǐng)求包含了普通的http請(qǐng)求、支持上傳、下載、socket?;旧蠞M足了我們開發(fā)中所需要的網(wǎng)絡(luò)需求。
這些API屬于邏輯層,寫在js文件中,
使用實(shí)例:
wx.getLocation({
type: 'wgs84',
success: function(res) {
var latitude = res.latitude
var longitude = res.longitude
var speed = res.speed
var accuracy = res.accuracy
}})
可以到官方文檔-API查看其它API的使用方法。
1、模擬器
可以在模擬器上看效果,上面降到了運(yùn)行底層不同,效果跟在手機(jī)上運(yùn)行有些差異

2、真機(jī)
在左邊的選項(xiàng)欄中,選擇項(xiàng)目,然后點(diǎn)預(yù)覽會(huì)生產(chǎn)一個(gè)二維碼,用管理員微信號(hào)掃一掃就可以在真機(jī)上看實(shí)際效果





能夠計(jì)算里程、時(shí)間、實(shí)時(shí)獲取跑步路徑(有些粗糙)
主要使用了微信小程序的獲取位置APIwx.getLocation()和地圖組件map。
首先實(shí)現(xiàn)一個(gè)計(jì)時(shí)器進(jìn)行 計(jì)時(shí),通過(guò)wx.getLocation()獲取坐標(biāo),把獲取到的坐標(biāo)存在一個(gè)數(shù)組中,通過(guò)坐標(biāo)每隔一段時(shí)間獲取里程,進(jìn)行累加得到總里程,同時(shí)也通過(guò)坐標(biāo)點(diǎn)進(jìn)行連線
存在的問(wèn)題:
1、因?yàn)槟壳罢也坏皆诘貓D上畫連線的方法,所以采用了在地圖上貼小紅點(diǎn)圖的方法顯示大概跑步路徑,路徑比較粗糙。
2、雖然采用了API里面的火星坐標(biāo)gcj02類型,但是獲取的坐標(biāo)跟國(guó)際坐標(biāo)差不多,依然存在著偏差。
我把全部代碼放在github上-weChatApp-Run,可以下載來(lái)看看或者先star收藏,我以后還會(huì)進(jìn)行一些優(yōu)化更新?,F(xiàn)在只是一個(gè)學(xué)習(xí)Demo,大家溝通學(xué)習(xí),實(shí)際應(yīng)用還需更多優(yōu)化。
wxml文件布局代碼:
<view class="head" style="flex-direction:row;">
<image class="icon" src="/resources/joyrun.png" mode="aspectFill"/>
<button bindtap="openLocation">打開位置</button>
<button bindtap="starRun">開始跑步</button>
<button bindtap="stopRun">暫停跑步</button>
<text>\\n里程數(shù):{{meters}}km</text>
<text>\\n\\n時(shí)間:{{time}}</text>
</view>
<view class="mainView">
<map
class="mapView"
style="width: 100%; height: 375px;"
latitude="{{latitude}}"
longitude="{{longitude}}"
markers="{{markers}}"
covers="{{covers}}"
>
</map>
</view>
js文件邏輯代碼:
var countTooGetLocation = 0;
var total_micro_second = 0;
var starRun = 0;
var totalSecond = 0;
var oriMeters = 0.0;
/* 毫秒級(jí)倒計(jì)時(shí) */
function count_down(that) {
if (starRun == 0) {
return;
}
if (countTooGetLocation >= 100) {
var time = date_format(total_micro_second);
that.updateTime(time);
}
if (countTooGetLocation >= 5000) { //1000為1s
that.getLocation();
countTooGetLocation = 0;
}
setTimeout
setTimeout(function(){
countTooGetLocation += 10;
total_micro_second += 10;
count_down(that);
}
,10
)
}
// 時(shí)間格式化輸出,如03:25:19 86。每10ms都會(huì)調(diào)用一次
function date_format(micro_second) {
// 秒數(shù)
var second = Math.floor(micro_second / 1000);
// 小時(shí)位
var hr = Math.floor(second / 3600);
// 分鐘位
var min = fill_zero_prefix(Math.floor((second - hr * 3600) / 60));
// 秒位
var sec = fill_zero_prefix((second - hr * 3600 - min * 60));// equal to => var sec = second % 60;
return hr + ":" + min + ":" + sec + " ";
}
function getDistance(lat1, lng1, lat2, lng2) {
var dis = 0;
var radLat1 = toRadians(lat1);
var radLat2 = toRadians(lat2);
var deltaLat = radLat1 - radLat2;
var deltaLng = toRadians(lng1) - toRadians(lng2);
var dis = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(deltaLat / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(deltaLng / 2), 2)));
return dis * 6378137;
function toRadians(d) { return d * Math.PI / 180;}
}
function fill_zero_prefix(num) {
return num < 10 ? "0" + num : num
}
//****************************************************************************************
//****************************************************************************************
Page({
data: {
clock: '',
isLocation:false,
latitude: 0,
longitude: 0,
markers: [],
covers: [],
meters: 0.00,
time: "0:00:00"
},
//****************************
onLoad:function(options){
// 頁(yè)面初始化 options為頁(yè)面跳轉(zhuǎn)所帶來(lái)的參數(shù)
this.getLocation()
console.log("onLoad")
count_down(this);
},
//****************************
openLocation:function (){
wx.getLocation({
type: 'gcj02', // 默認(rèn)為 wgs84 返回 gps 坐標(biāo),gcj02 返回可用于 wx.openLocation 的坐標(biāo)
success: function(res){
wx.openLocation({
latitude: res.latitude, // 緯度,范圍為-90~90,負(fù)數(shù)表示南緯
longitude: res.longitude, // 經(jīng)度,范圍為-180~180,負(fù)數(shù)表示西經(jīng)
scale: 28, // 縮放比例
})
},
})
},
//****************************
starRun :function () {
if (starRun == 1) {
return;
}
starRun = 1;
count_down(this);
this.getLocation();
},
//****************************
stopRun:function () {
starRun = 0;
count_down(this);
},
//****************************
updateTime:function (time) {
var data = this.data;
data.time = time;
this.data = data;
this.setData ({
time : time,
})
},
//****************************
getLocation:function () {
var that = this
wx.getLocation({
type: 'gcj02', // 默認(rèn)為 wgs84 返回 gps 坐標(biāo),gcj02 返回可用于 wx.openLocation 的坐標(biāo)
success: function(res){
console.log("res----------")
console.log(res)
//make datas
var newCover = {
latitude: res.latitude,
longitude: res.longitude,
iconPath: '/resources/redPoint.png',
};
var oriCovers = that.data.covers;
console.log("oriMeters----------")
console.log(oriMeters);
var len = oriCovers.length;
var lastCover;
if (len == 0) {
oriCovers.push(newCover);
}
len = oriCovers.length;
var lastCover = oriCovers[len-1];
console.log("oriCovers----------")
console.log(oriCovers,len);
var newMeters = getDistance(lastCover.latitude,lastCover.longitude,res.latitude,res.longitude)/1000;
if (newMeters < 0.0015){
newMeters = 0.0;
}
oriMeters = oriMeters + newMeters;
console.log("newMeters----------")
console.log(newMeters);
var meters = new Number(oriMeters);
var showMeters = meters.toFixed(2);
oriCovers.push(newCover);
that.setData({
latitude: res.latitude,
longitude: res.longitude,
markers: [],
covers: oriCovers,
meters:showMeters,
});
},
})
}
})
本文是一個(gè)快速上手開發(fā)的介紹,細(xì)節(jié)介紹可以查看官方文檔