環境搭建
-
注冊,獲取APPID(沒有這個不能真雞調試)
-
下載微信web開發者工具(挺多bug,將就用)
-
打開微信web開發者工具,掃碼登錄,新建小程序,輸入APPID,勾選創建quick start項目。
工程結構
可以看到工程根目錄中有個app.js,這里可以定義全局變量,通過getApp()獲取。
項目中有了一些示例,已經有了獲取用戶信息的方法等。
開發地圖定位,選擇位置功能
我們直接修改index頁面來做這個功能。
準備
-
新建imgs目錄,加入2個圖標(ic_location和ic_position),用于標記當前位置,和地圖中央位置。
-

-

添加定位功能
修改app.js,加入定位功能,獲取當前位置。
App({
onLaunch: function () {
var logs = wx.getStorageSync('logs') || []
logs.unshift(Date.now())
wx.setStorageSync('logs', logs)
}
,getUserInfo:function(cb){
var that = this
if(this.globalData.userInfo){
typeof cb == "function" && cb(this.globalData.userInfo)
}else{
wx.login({
success: function () {
wx.getUserInfo({
success: function (res) {
that.globalData.userInfo = res.userInfo
typeof cb == "function" && cb(that.globalData.userInfo)
}
})
}
})
}
}
,getLocationInfo: function(cb){
var that = this;
if(this.globalData.locationInfo){
cb(this.globalData.locationInfo)
}else{
wx.getLocation({
type: 'gcj02',
success: function(res){
that.globalData.locationInfo = res;
cb(that.globalData.locationInfo)
},
fail: function() {
},
complete: function() {
}
})
}
}
,globalData:{
userInfo:null
,locationInfo: null
}
})