Browse Source

--热门城市增加定位--

featrue/v4.5
DESKTOP-A1SENDA\HUzy 4 years ago
parent
commit
416ea4d3d2
7 changed files with 150 additions and 38 deletions
  1. 2
      app.js
  2. 6
      app.json
  3. 86
      components/city-change/index.js
  4. 16
      components/city-change/index.wxml
  5. 8
      components/city-change/index.wxss
  6. 69
      utils/map.js
  7. 1
      utils/qqmap-wx-jssdk.min.js

2
app.js

@ -1,7 +1,7 @@
//app.js //app.js
App({ App({
//----------------------------------------------globalData-------------------------------------- //----------------------------------------------globalData--------------------------------------
evn: 0,//0:开发环境,1:测试环境,2:生产环境
evn: 2,//0:开发环境,1:测试环境,2:生产环境
tmplIds: ['SUjEgwDopCv9xkkSZ4KbS0L7XbAiVQor6GmPg14K760'], tmplIds: ['SUjEgwDopCv9xkkSZ4KbS0L7XbAiVQor6GmPg14K760'],
agentMsgIds: ['kG8DErWDpyzBHCFaLlSKYMF7xVy8UpgogCwV_WSNt10', 'lOQ8Gvyy_dTk68bYGpRVnVA0M7DsYYrV81Gd39GUPBA'], agentMsgIds: ['kG8DErWDpyzBHCFaLlSKYMF7xVy8UpgogCwV_WSNt10', 'lOQ8Gvyy_dTk68bYGpRVnVA0M7DsYYrV81Gd39GUPBA'],
version: 152, version: 152,

6
app.json

@ -110,6 +110,12 @@
"desc": "你的位置信息将用获取最近的打包站" "desc": "你的位置信息将用获取最近的打包站"
} }
}, },
"plugins": {
"chooseLocation": {
"version": "1.0.6",
"provider": "wx76a9a06e5b4e693e"
}
},
"window": { "window": {
"navigationBarBackgroundColor": "#FFFFFF", "navigationBarBackgroundColor": "#FFFFFF",
"navigationBarTitleText": "", "navigationBarTitleText": "",

86
components/city-change/index.js

@ -1,4 +1,4 @@
// import qqmap from '../../utils/map.js'; //如果需要定位打开wxml和js中注释部分,替换此处map.js为实际引用
import qqmap from '../../utils/map';
import region2 from '../../utils/region2' import region2 from '../../utils/region2'
const app = getApp() const app = getApp()
Component({ Component({
@ -26,11 +26,31 @@ Component({
touchEndy:0, touchEndy:0,
rightheight:0, rightheight:0,
topHeight:0, topHeight:0,
dialogIsShow:false
dialogIsShow:false,
chooseCity:null,//缓存的城市
}, },
attached() { attached() {
var that=this var that=this
this.data.topHeight=app.globalData.CustomBar this.data.topHeight=app.globalData.CustomBar
wx.getStorage({
key: 'city',
success: function(res){
if(res.data.cityName && res.data.cityId){
that.setData({
chooseCity:res.data.cityName
})
}else{
that.setData({
chooseCity:''
})
}
},
fail: function(res) {
that.setData({
chooseCity:''
})
}
});
var query = this.createSelectorQuery() var query = this.createSelectorQuery()
query.select('#rightSortHeight').boundingClientRect(function (res) { query.select('#rightSortHeight').boundingClientRect(function (res) {
console.log(res); console.log(res);
@ -69,9 +89,13 @@ Component({
break; break;
} }
if (city) { if (city) {
var cityData={
cityName: city,
cityId:cityId
}
wx.setStorage({ wx.setStorage({
key: 'city', key: 'city',
data: city
data: cityData
}) //点击后给父组件可以通过bindcitytap事件,获取到cityname的值,这是子组件给父组件传值和触发事件的方法 }) //点击后给父组件可以通过bindcitytap事件,获取到cityname的值,这是子组件给父组件传值和触发事件的方法
this.triggerEvent('citytap', { this.triggerEvent('citytap', {
cityname: city, cityname: city,
@ -93,22 +117,22 @@ Component({
}, },
//调用定位 //调用定位
// getLocate(){
// let that=this;
// new qqmap().getLocateInfo().then(function (val) {//这个方法在另一个文件里,下面有贴出代码
// console.log(val);
// if (val.indexOf('市') !== -1) {//这里是去掉“市”这个字
// console.log(val.indexOf('市') - 1);
// val = val.slice(0, val.indexOf('市'));
// console.log(val);
// }
// that.setData({
// locateCity: val
// });
// //把获取的定位和获取的时间放到本地存储
// wx.setStorageSync('locatecity', { city: val, time: new Date().getTime() });
// });
// }
getLocate(){
let that=this;
new qqmap().getLocateInfo().then(function (val) {//这个方法在另一个文件里,下面有贴出代码
console.log(val);
if (val.indexOf('市') !== -1) {//这里是去掉“市”这个字
console.log(val.indexOf('市') - 1);
val = val.slice(0, val.indexOf('市'));
console.log(val);
}
that.setData({
locateCity: val,
});
//把获取的定位和获取的时间放到本地存储
wx.setStorageSync('locatecity', { city: val, time: new Date().getTime() });
});
},
//开始触摸事件 //开始触摸事件
touchStart: function (e) { touchStart: function (e) {
this.data.touchEndy = e.touches[0].pageY-this.data.topHeight; this.data.touchEndy = e.touches[0].pageY-this.data.topHeight;
@ -150,17 +174,17 @@ Component({
}, },
}, },
ready() { ready() {
// console.log(getApp());
// let that = this,
// cityOrTime = wx.getStorageSync('locatecity')||{},
// time = new Date().getTime(),
// city='';
// if (!cityOrTime.time||(time - cityOrTime.time > 1800000)){//每隔30分钟请求一次定位
// this.getLocate();
// }else{//如果未满30分钟,那么直接从本地缓存里取值
// that.setData({
// locateCity: cityOrTime.city
// })
// }
console.log(getApp());
let that = this,
cityOrTime = wx.getStorageSync('locatecity')||{},
time = new Date().getTime(),
city='';
if (!cityOrTime.time||(time - cityOrTime.time > 1800000)){//每隔30分钟请求一次定位
this.getLocate();
}else{//如果未满30分钟,那么直接从本地缓存里取值
that.setData({
locateCity: cityOrTime.city
})
}
}, },
}) })

16
components/city-change/index.wxml

@ -1,10 +1,18 @@
<view class='city_box' style='{{styles}}'> <view class='city_box' style='{{styles}}'>
<view class='city_left'> <view class='city_left'>
<scroll-view scroll-y style='width:100%;height:85vh;' scroll-with-animation scroll-into-view="{{cityListId}}"> <scroll-view scroll-y style='width:100%;height:85vh;' scroll-with-animation scroll-into-view="{{cityListId}}">
<!-- <view class='city_locate' data-types='locate' catchtap='cityTap'>
<text class='city_locate_title'>自动定位</text>
<text class='city_locate_text' style='{{!locateCity&&"color:#33b9ff;"}}'>{{locateCity||'点击定位'}}</text>
</view> -->
<view class='city_locate'>
<view data-types='locate' catchtap='cityTap'>
<text class='city_locate_title'>自动定位</text>
<text class='city_locate_text'
style='color:#33b9ff;'>{{locateCity||'点击定位'}}</text>
</view>
<view >
<text class='city_locate_title' >选择城市</text>
<text class='city_locate_text' style='{{!locateCity&&"color:#33b9ff;"}}'>{{chooseCity}}</text>
</view>
</view>
<view class='new_city'> <view class='new_city'>
<view class='new_city_title'>热门城市</view> <view class='new_city_title'>热门城市</view>
<view class='new_city_box'> <view class='new_city_box'>

8
components/city-change/index.wxss

@ -23,7 +23,11 @@
text-align: center; text-align: center;
} }
.city_locate,
.city_locate{
display: flex;
justify-content: space-between;
padding: 10rpx 20rpx 10rpx 20rpx;
}
.national { .national {
height: 80rpx; height: 80rpx;
line-height: 80rpx; line-height: 80rpx;
@ -99,4 +103,4 @@
padding-bottom: 30rpx; padding-bottom: 30rpx;
text-align: center; text-align: center;
box-shadow:0px 2px 5px 5px gray; box-shadow:0px 2px 5px 5px gray;
}
}

69
utils/map.js

@ -0,0 +1,69 @@
const wxqqmap = require('../utils/qqmap-wx-jssdk.min'),
qqwxmap = new wxqqmap({
key: 'GAMBZ-CBGCK-GB6J3-A6PJX-F6ZHH-IWFUD' // 必填,这里最好填自己申请的的
// key: 'NGOBZ-UL7ED-WJ34D-PMWW7-LBDI2-J5FYK'
});
export default class qqmap{//获取定位信息
getLocateInfo(){
let that=this;
return new Promise(function (resolve, reject) {
that.location().then(function(val){
//如果通过授权,那么直接使用腾讯的微信小程序sdk获取当前定位城市
qqwxmap.reverseGeocoder({
location: {
latitude: val.latitude,
longitude: val.longitude
},
success: function (res) {
console.log(res.result.address_component.city);
resolve(res.result.address_component.city);//返回城市
},
fail: function (res) {
reject(res);
},
complete: function (res) {
console.log(res);
}
});
},function(error) {
//如果用户拒绝了授权,那么这里会提醒他,去授权后再定位
console.log('shibai');
wx.showModal({
title: '',
content: '自动定位需要授权地理定位选项',
confirmText: '去授权',
success(res) {
if (res.confirm) {
wx.openSetting({
success(res) {
console.log(res);
that.getLocateInfo();
}
})
}
}
})
})
})
}
//定位,获取当前经纬度
location(){
return new Promise(function (resolve, reject) {
wx.getLocation({
altitude: true,
success: function (res) {
resolve(res);
},fail(res){
reject(res);
}
})
});
}
}

1
utils/qqmap-wx-jssdk.min.js
File diff suppressed because it is too large
View File

Loading…
Cancel
Save