首發地址javascript
一、註冊一個小程序帳號,得用一個沒註冊過公衆號的郵箱註冊。
二、註冊過程當中須要不少認證,有不少認證,比較繁瑣,若是暫時只是開發測試,不進行提審、發佈的話,只要完成營業執照號填寫就能夠了,不須要完成微信認證。
三、註冊完帳號,登陸,在主頁面左邊列表中點擊設置,而後再設置頁面中選開發設置就能夠看到AppID
,用於登陸開發工具。css
能夠到官網下載開發工具下載html
打開開發者工具,選擇小程序選項,到達添加項目頁面
這個時候在前面設置頁面的AppId
就用到了。java
若是項目目錄中的文件是個空文件夾,會提示是否建立quick start 項目。
選擇「是」,開發者工具會幫助咱們在開發目錄裏生成一個簡單的 demo。
這個Demo擁有一個完整的小程序的大概框架。git
先看下一目錄:
github
app.js: 小程序邏輯,生命週期,,全局變量
app.json: 小程序公共設置,導航欄顏色等,不能夠註釋
app.wxss :小程序公共樣式,類CSS 。chrome
每個小程序頁面是由同路徑下同名的四個不一樣後綴文件的組成,如:index.js、index.wxml、index.wxss、index.json。json
微信小程序中的每個頁面的【路徑+頁面名】都須要寫在 app.json 的 pages 中,且 pages 中的第一個頁面是小程序的首頁。canvas
這四個文件按照功能能夠分紅三個部分:小程序
配置:json 文件
邏輯層:js文件
視圖層:wxss.wxml文件
在 iOS 上,小程序的 javascript 代碼是運行在 JavaScriptCore 中
在 Android 上,小程序的 javascript 代碼是經過 X5 內核來解析
在 開發工具上, 小程序的 javascript 代碼是運行在 nwjs(chrome內核) 中。因此開發工具上的效果跟實際效果有所出入。
微信提供了許多組件,主要分爲八種:
視圖容器、
基礎內容、
表單組件、
操做反饋、
導航、
媒體組件、
地圖、
畫布
包含view、scroll-view、button、form等普一般用的組件,也提供了地圖map
、畫布canvas
。
組件主要屬於視圖層,經過wxml來進行結構佈局,相似於html。經過wxss修改樣式,相似於css。
組件使用語法實例:
<!--普通視圖--> <view>這是一個普通視圖</view> <!--wxss樣式修改--> <view clas="mainView">樣式修改過的視圖</view>
更多的組件以及相關使用方法能夠到官方文檔-組件查看
網絡
媒體
數據
位置
設備
界面
開發接口
其中網絡請求
的使用必須先到公衆平臺登陸小程序帳號,在設置頁面那裏,設置容許訪問的域名,網絡請求包含了普通的http請求、支持上傳、下載、socket。基本上知足了咱們開發中所須要的網絡需求。
這些API屬於邏輯層,寫在js文件中,
使用實例:
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的使用方法。
一、模擬器
能夠在模擬器上看效果,上面降到了運行底層不一樣,效果跟在手機上運行有些差別
二、真機
在左邊的選項欄中,選擇項目,而後點預覽會生產一個二維碼,用管理員微信號掃一掃就能夠在真機上看實際效果
可以計算里程、時間、實時獲取跑步路徑(有些粗糙)
主要使用了微信小程序的獲取位置APIwx.getLocation()
和地圖組件map
。
首先實現一個計時器進行 計時,經過wx.getLocation()
獲取座標,把獲取到的座標存在一個數組中,經過座標每隔一段時間獲取里程,進行累加獲得總里程,同時也經過座標點進行連線
存在的問題:
一、由於目前找不到在地圖上畫連線的方法,因此採用了在地圖上貼小紅點圖的方法顯示大概跑步路徑,路徑比較粗糙。
二、雖然採用了API裏面的火星座標gcj02類型,可是獲取的座標跟國際座標差很少,依然存在着誤差。
我把所有代碼放在github上-weChatApp-Run,能夠下載來看看或者先star收藏,我之後還會進行一些優化更新。如今只是一個學習Demo,你們溝通學習,實際應用還需更多優化。
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里程數:{{meters}}km</text> <text>\n\n時間:{{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; /* 毫秒級倒計時 */ 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 ) } // 時間格式化輸出,如03:25:19 86。每10ms都會調用一次 function date_format(micro_second) { // 秒數 var second = Math.floor(micro_second / 1000); // 小時位 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){ // 頁面初始化 options爲頁面跳轉所帶來的參數 this.getLocation() console.log("onLoad") count_down(this); }, //**************************** openLocation:function (){ wx.getLocation({ type: 'gcj02', // 默認爲 wgs84 返回 gps 座標,gcj02 返回可用於 wx.openLocation 的座標 success: function(res){ wx.openLocation({ latitude: res.latitude, // 緯度,範圍爲-90~90,負數表示南緯 longitude: res.longitude, // 經度,範圍爲-180~180,負數表示西經 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', // 默認爲 wgs84 返回 gps 座標,gcj02 返回可用於 wx.openLocation 的座標 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, }); }, }) } })
本文是一個快速上手開發的介紹,細節介紹能夠查看官方文檔
個人相關所有代碼放在github上-weChatApp-Run