Skip to content

Commit

Permalink
修改承载页定位功能
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaobinwu committed Apr 27, 2017
1 parent 4707a45 commit ff9dbd2
Show file tree
Hide file tree
Showing 24 changed files with 288 additions and 120 deletions.
4 changes: 3 additions & 1 deletion app.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
{
"pages":[
"pages/index/index",
"pages/order/order",
"pages/home/home",
"pages/list/list",
"pages/more/more",
"pages/center/center",
"pages/logs/logs"
],
Expand Down
1 change: 0 additions & 1 deletion app.wxss
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@
flex-direction: column;
align-items: center;
justify-content: space-between;
padding: 200rpx 0;
box-sizing: border-box;
}
Binary file added image/loading-super.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 19 additions & 0 deletions pages/home/home.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// pages/home/home.js
Page({
data:{},
onLoad:function(options){
// 页面初始化 options为页面跳转所带来的参数
},
onReady:function(){
// 页面渲染完成
},
onShow:function(){
// 页面显示
},
onHide:function(){
// 页面隐藏
},
onUnload:function(){
// 页面关闭
}
})
1 change: 1 addition & 0 deletions pages/home/home.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
2 changes: 2 additions & 0 deletions pages/home/home.wxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<!--pages/home/home.wxml-->
<text>pages/home/home.wxml</text>
1 change: 1 addition & 0 deletions pages/home/home.wxss
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* pages/home/home.wxss */
80 changes: 19 additions & 61 deletions pages/index/index.js
Original file line number Diff line number Diff line change
@@ -1,66 +1,24 @@
//index.js
//获取应用实例
/* 承载页 */
var app = getApp()

// 引入SDK核心类
var QQMapWX = require('../../lib/qqmap-wx-jssdk.min.js');

// 实例化API核心类
var demo = new QQMapWX({
key: 'LHVBZ-DQVWW-C2YRC-REKYI-HRUV7-JPFYV' // 必填
});


var bearer = require('../../utils/bearer.js');
var util = require('../../utils/util.js');
var ports = require('../../utils/ports.js');
Page({
data: {
motto: 'Hello World',
userInfo: {}
},
//事件处理函数
bindViewTap: function() {
wx.navigateTo({
url: '../logs/logs'
})
},
onLoad: function () {
console.log('onLoad')
var that = this
//调用应用实例的方法获取全局数据
app.getUserInfo(function(userInfo){
//更新数据
that.setData({
userInfo:userInfo
})
});

// // 调用接口
// demo.reverseGeocoder({
// location: {
// latitude: 39.984060,
// longitude: 116.307520
// },
// success: function(res) {
// console.log(res);
// },
// fail: function(res) {
// console.log(res);
// },
// complete: function(res) {
// console.log(res);
// }
// });

wx.request({
url: 'https://www.wzhouhui.com/dj/Location/getLocation', //仅为示例,并非真实的接口地址
method: 'post',
data: {
lng: '116.307520' ,
lat: '39.984060'
},
success: function(res) {
console.log(res)
}
});

bearer.init();
var ajaxCfg={
method: 'POST',
url: ports.getLocation,
data:{
lat:22.516232,
lng:113.921143,
location_addr: "粤海大厦",
region_id: "77",
region_name:"深圳市"
}
}
util.wxRequest(ajaxCfg, true).then(res =>{
console.log(res)
})
}
})
10 changes: 1 addition & 9 deletions pages/index/index.wxml
Original file line number Diff line number Diff line change
@@ -1,10 +1,2 @@
<!--index.wxml-->
<view class="container">
<view bindtap="bindViewTap" class="userinfo">
<image class="userinfo-avatar" src="{{userInfo.avatarUrl}}" background-size="cover"></image>
<text class="userinfo-nickname">{{userInfo.nickName}}</text>
</view>
<view class="usermotto">
<text class="user-motto">{{motto}}</text>
</view>
</view>
<image src="../../image/loading-super.gif"></image>
25 changes: 7 additions & 18 deletions pages/index/index.wxss
Original file line number Diff line number Diff line change
@@ -1,21 +1,10 @@
/**index.wxss**/
.userinfo {
display: flex;
flex-direction: column;
align-items: center;
}

.userinfo-avatar {
width: 128rpx;
height: 128rpx;
margin: 20rpx;
border-radius: 50%;
}

.userinfo-nickname {
color: #aaa;
}

.usermotto {
margin-top: 200px;
image{
width: 60rpx;
height: 60rpx;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
19 changes: 19 additions & 0 deletions pages/list/list.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// pages/list/list.js
Page({
data:{},
onLoad:function(options){
// 页面初始化 options为页面跳转所带来的参数
},
onReady:function(){
// 页面渲染完成
},
onShow:function(){
// 页面显示
},
onHide:function(){
// 页面隐藏
},
onUnload:function(){
// 页面关闭
}
})
1 change: 1 addition & 0 deletions pages/list/list.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
2 changes: 2 additions & 0 deletions pages/list/list.wxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<!--pages/list/list.wxml-->
<text>pages/list/list.wxml</text>
1 change: 1 addition & 0 deletions pages/list/list.wxss
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* pages/list/list.wxss */
6 changes: 3 additions & 3 deletions pages/logs/logs.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ Page({
},
onLoad: function (options) {
console.log(options);
var a = [];
var arr = [];
for(var key in options){
a.push(key);
arr.push({idx: key, opt: options[key]});
}
this.setData({
logs: (wx.getStorageSync('logs') || []).map(function (log) {
return util.formatTime(new Date(log))
}),
logText: a
logText: arr
})
}
})
2 changes: 1 addition & 1 deletion pages/logs/logs.wxml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<!--logs.wxml-->
<view class="container log-list">
<block wx:for="{{logText}}">
<text>{{item}}</text>
<text>{{item.idx}}:{{item.opt}}</text>
</block>

<block wx:for="{{logs}}" wx:for-item="log" wx:key="*this">
Expand Down
19 changes: 19 additions & 0 deletions pages/more/more.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// pages/more/more.js
Page({
data:{},
onLoad:function(options){
// 页面初始化 options为页面跳转所带来的参数
},
onReady:function(){
// 页面渲染完成
},
onShow:function(){
// 页面显示
},
onHide:function(){
// 页面隐藏
},
onUnload:function(){
// 页面关闭
}
})
1 change: 1 addition & 0 deletions pages/more/more.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
2 changes: 2 additions & 0 deletions pages/more/more.wxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<!--pages/more/more.wxml-->
<text>pages/more/more.wxml</text>
1 change: 1 addition & 0 deletions pages/more/more.wxss
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* pages/more/more.wxss */
25 changes: 14 additions & 11 deletions utils/address.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ var qqmapsdk = new QQMapWX({
/*获取gps坐标 */
function getCoords(){
var getLocationPromisified = util.wxPromisify(wx.getLocation);
getLocationPromisified({
return getLocationPromisified({
type: 'wgs84'
}).then(function(res){
return Promise.resolve({
Expand All @@ -24,7 +24,7 @@ function getCoords(){
});
}).catch(function(err){
wx.showToast({
title: 'get location failed',
title: '您不允许获取您的位置',
duration: 2000
});
});
Expand All @@ -35,7 +35,7 @@ function getCoords(){
*/
function saveGPSInfo(info){
// 将GPS信息存储以后面备用
util.setStoreage('gps_info',JSON.stringify(info));
util.setStorage('gps_info',JSON.stringify(info));
}

/**
Expand All @@ -52,37 +52,34 @@ function getGPSInfo(){
region_name:''
};
getCoords().then((point)=>{
console.log(point)
// 请求用户授权定位
//逆地址解析
var reverseGeocoder = util.wxPromisify(qqmapsdk.reverseGeocoder);
return reverseGeocoder({
var ReverseGeocoder = util.wxPromisify(qqmapsdk.reverseGeocoder, qqmapsdk); //需改变作用域
return ReverseGeocoder({
location:{
latitude: point.lat,
longitude: point.lng
}
});
}).then((res)=>{// 拿到腾讯地图解析过后的数据
// console.log(res)
// 经度
data.lng = res.result.ad_info.location.lng;
// 纬度
data.lat=res.result.ad_info.location.lat;
//详细地址
data.location_addr = res.result.address;

return region.getRegionId(res.result.address_component.city);

}).then((region)=>{// 通过城市名拿到城市区域id后

data.region_id = region.value;
data.region_name = region.name;

// 全量式返回定位结果
saveGPSInfo(data);
resolve({data});

}).catch((err)=>{

console.warn('address:getGPSInfo=>',err);
// 增量式返回定位结果
saveGPSInfo(data);
// 结果无论怎样都需要返回后端进行统一处理,所以不用reject
Expand All @@ -105,7 +102,7 @@ function getLocation(options){
needToken = 'token' in options ? options.token : true;
// 如果本地缓存token,则使用
if(token && needToken){
ajaxCfg.headers={'X-Auth-Token':token};
ajaxCfg.header={'X-Auth-Token':token};
}
return new Promise((resolve,reject)=>{
// 如果有已存在的定位信息(一般来自本地存储)
Expand All @@ -122,9 +119,15 @@ function getLocation(options){
if(result.err){
options.gpsError && options.gpsError(result.err);
}
if(ajaxCfg.header){
ajaxCfg.header.push({'content-type': 'application/x-www-form-urlencoded'});
}else{
ajaxCfg.header={'content-type': 'application/x-www-form-urlencoded'};
}
ajaxCfg.data= result.data;
return util.wxRequest(ajaxCfg, true);
}).then(result=>{
console.log(result);
resolve(result);
}).catch(err=>{
reject(err);
Expand Down
Loading

0 comments on commit ff9dbd2

Please sign in to comment.