123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472 |
- // pages/jsCase/citySel/index.js
- const {
- cityData
- } = require('./city.js')
- var map = require('../../../utils/map.js');
- var util = require('../../../utils/util.js');
- var api = require('../../../config/api.js');
- var app = getApp();
- Page({
- data: {
- index: 0,
- current_city: "当前定位",
- search: "",
- hot_city: [{
- name: "上海"
- }, {
- name: "北京"
- }, {
- name: "深圳"
- }, {
- name: "南京"
- }, {
- name: "苏州"
- }, {
- name: "杭州"
- }, {
- name: "青岛"
- }, {
- name: "广州"
- }, {
- name: "成都"
- }, {
- name: "重庆"
- }, {
- name: "昆山"
- }, {
- name: "武汉"
- }, {
- name: "西安"
- }, {
- name: "长沙"
- }],
- city_list: [],
- barHeight: 0,
- curr: -1,
- scrollViewId: "",
- barTop: 0,
- showLetter: true,
- result: [], //搜索结果
- mapShow: true,
- resultShow: false,
- cityShow: false,
- currentMarker: [
- // {longitude: 116.40159,
- // latitude: 39.90511,
- // title: '当前位置',}
- ],
- lng: '',
- lat: '',
- poiList: [],
- timer: '',
- loadTime: 0,
- isTouchMap: true
- },
- onUnload: function () {
- // 页面关闭
- clearInterval(this.data.timer);
- },
- onLoad: function (options) {
- // this.getUserLocation();
- let timer = setInterval(() => {
- if (this.data.loadTime < 10) {
- this.setData({
- loadTime: this.data.loadTime + 1
- });
- } else {
- clearInterval(that.data.timer);
- }
- }, 1000);
- this.setData({
- timer: timer
- });
- this.setData({
- city_list: cityData
- })
- wx.getSystemInfo({
- success: (res) => {
- console.log(res);
- let winHeight = res.windowHeight
- let barHeight = winHeight - res.windowWidth / 750 * 300;
- this.setData({
- barHeight: barHeight,
- barTop: res.windowWidth / 750 * 180,
- })
- }
- });
- this.mapCtx = wx.createMapContext("myMap");
- let that = this;
- console.log(options);
- if (options.lng && options.lat) {
- that.setData({
- addressName: options.addressname,
- current_city: options.city,
- lng: options.lng,
- lat: options.lat,
- });
- that.getPoiList();
- } else {
- // wx.authorize({
- // scope: "scope.userLocation",
- // success(res) {
- // console.log("已允许授权定位权限: ", res);
- wx.getLocation({
- type: 'wgs84',
- success: function (res) {
- let latitude = res.latitude;
- let longitude = res.longitude;
- app.globalData.lat = latitude; //
- app.globalData.lng = longitude; //把onload定位时候的经纬度存到全局
- app.globalData.currentLocationName = res.city;
- app.globalData.city = res.city;
- that.setData({
- addressName: app.globalData.currentLocationName,
- current_city: app.globalData.city,
- lng: app.globalData.lng,
- lat: app.globalData.lat,
- });
- console.log('当前经纬度:' + longitude + ',' + latitude);
- that.getPoiList();
- },
- fail: function (res) {
- console.log('fail' + JSON.stringify(res));
- }
- });
- // }
- // });
- }
- },
- onReady() {
- },
- getUserLocation: function () {
- map.getUserLocation();
- console.log(app.globalData);
- let that = this;
- setTimeout(() => {
- that.setData({
- addressName: app.globalData.currentLocationName,
- current_city: app.globalData.city,
- lng: app.globalData.lng,
- lat: app.globalData.lat,
- });
- this.getPoiList();
- }, 1000);
- },
- /**
- * 获取value值
- * @param {*} e
- */
- getValue(e) {
- this.setData({
- search: e.detail.value
- }, () => {
- // this.search(e.detail.value);
- })
- },
- confSearch(e){
- this.search(this.data.search);
- },
- /**
- * 搜索成功
- */
- search(e) {
- // let result = [], { city_list } = this.data;
- // city_list.forEach((item1) => {
- // item1.data.forEach((item2) => {
- // if (item2.keyword.indexOf(e.toLocaleUpperCase()) !== -1) {
- // result.push({ name: item2.cityName })
- // }
- // })
- // })
- // this.setData({
- // result,
- // })
- util.request(api.gdSearchUrl, {
- keywords: e,
- output: 'JSON',
- city: this.data.current_city,
- key: api.gdKey
- }).then(res => {
- console.log(res);
- if (res.info == 'OK') {
- let list = res.tips;
- let result = list;
- if (list.length > 1) {
- // result=list.splice(1);
- this.setData({
- result: result
- });
- } else {
- this.setData({
- result: []
- });
- }
- }
- });
- },
- /**
- * 清空验证码
- */
- clear_input() {
- this.setData({
- search: "",
- mapShow: true,
- cityShow: false,
- resultShow: false,
- })
- },
- touch(e) {
- let pageY = e.touches[0].pageY
- let index = Math.floor((pageY - this.data.barTop) / (this.data.barHeight / 22)) //向下取整
- let item = this.data.city_list[index]
- if (item) {
- this.setData({
- scrollViewId: item.letter,
- curr: index
- })
- }
- },
- touchStart(e) {
- this.setData({
- showLetter: true
- })
- this.touch(e)
- },
- touchMove(e) {
- this.touch(e)
- },
- touchEnd() {
- this.setData({
- showLetter: false,
- })
- },
- touchCancel() {
- this.setData({
- showLetter: false,
- })
- },
- mapFocus() {
- this.setData({
- mapShow: false,
- resultShow: true,
- cityShow: false,
- result: []
- });
- },
- switchCity() {
- if (this.data.cityShow) {
- this.setData({
- mapShow: true,
- cityShow: false,
- resultShow: false
- });
- } else {
- this.setData({
- mapShow: false,
- cityShow: true,
- resultShow: false,
- search: ''
- });
- }
- },
- msg(title, duration = 1500) {
- wx.showToast({
- title,
- duration,
- icon: 'none'
- });
- },
- regionChange(e) {
- console.log('isTouchMap', this.data.isTouchMap);
- if (this.data.isTouchMap == false) return;
- console.log('region变化' + this.data.index + ':' + e.detail.type, e);
-
- // this.msg(this.data.index+'');
- // this.msg(`region: latitude: ${ latitude }, longitude: ${ longitude }`);
- // 经度范围:73°33′E至135°05′E 东经
- // 纬度范围:3°51′N至53°33′N 北纬
- //type为end才有centerLocation
- // && longitude > 72 && longitude < 136 && latitude > 3 && latitude < 54
- if (this.data.loadTime > 3 && e.detail.type == 'end' ) {
- const {
- centerLocation
- } = e.detail;
- let latitude = Number(centerLocation.latitude).toFixed(4);
- let longitude = Number(centerLocation.longitude).toFixed(4);
- console.log('当前中心点经纬度', latitude, longitude);
-
- if (latitude == this.data.lat) return;
- this.setData({
- lng: longitude,
- lat: latitude,
- index: this.data.index + 1
- });
- this.getPoiList();
- // }
- }
- // this.setData({
- // currentMarker:[
- // {longitude:Number(longitude),
- // latitude:Number(latitude),
- // // title: '',
- // }
- // ]
- // });
- },
- mapTap(e) {
- console.log(e.detail);
- },
- getPoiList() {
- util.request(api.gdUrl, {
- // location:'116.40159,39.90511',
- location: this.data.lng + ',' + this.data.lat,
- extensions: 'all',
- // poitype:'',
- key: api.gdKey,
- radius: 3000,
- }).then(res => {
- console.log('poi', res);
- if (res.info == 'OK') {
- let list = res.regeocode.pois;
- var t = '';
- var length = list.length;
- for (var i = 0; i < length; i++) {
- for (var j = 0; j < length - i - 1; j++) {
- if (Number(list[j].distance) > Number(list[j + 1].distance)) {
- t = list[j];
- list[j] = list[j + 1];
- list[j + 1] = t;
- }
- }
- }
- this.setData({
- currentName: list.length > 0 ? list[0].name : res.regeocode.addressComponent.streetNumber.street,
- poiList: list,
- })
- }
- });
- },
- resultTap(e) {
- // adcode: "110101"
- // address: "1号线/八通线"
- // city: []
- // district: "北京市东城区"
- // id: "BV10006499"
- // location: "116.401216,39.907780"
- // name: "天安门东(地铁站)"
- // typecode: "150500"
- let location = e.currentTarget.dataset.location;
- let name = e.currentTarget.dataset.name;
- this.setData({
- currentName: name,
- lng: location.split(',')[0],
- lat: location.split(',')[1],
- resultShow: false,
- mapShow: true,
- });
- this.getPoiList();
- },
- cityTap(e) {
- // this.setData({
- // isTouchMap: false
- // });
- console.log('isTouchMap', this.data.isTouchMap);
- let city = e.currentTarget.dataset.city;
- console.log(city);
- util.request(api.gdCityUrl, {
- address: city,
- output: 'JSON',
- key: api.gdKey,
- }).then(res => {
- console.log(res);
- if (res.info == 'OK') {
- let location = res.geocodes[0].location;
- this.setData({
- current_city: city,
- lng: location.split(',')[0],
- lat: location.split(',')[1],
- cityShow: false,
- mapShow: true,
- });
- this.getPoiList();
- // setTimeout(() => {
- // this.setData({
- // isTouchMap: true
- // })
- // }, 500);
- }
- });
- },
- poiTap(e) {
- // address: "菖蒲河沿9号(天安门东地铁站B东北口步行200米)"
- // businessarea: "东单"
- // direction: "东"
- // distance: "210.116"
- // id: "B0FFFLJW2Q"
- // location: "116.403456,39.908566"
- // name: "菖蒲河公园"
- // poiweight: "0.426156"
- // tel: []
- // type: "风景名胜;公园广场;公园"
- let poi = e.currentTarget.dataset.poi;
- console.log(poi);
- app.globalData.mapSelectFinish = true;
- app.globalData.mapSelectPoi = poi;
- wx.navigateBack();
- },
- mapUpdated(e) {
- console.log('mapupdated');
- console.log(e);
- // this.mapCtx = wx.createMapContext("myMap");
- this.mapCtx.getCenterLocation({
- success: (res) => {
- console.log(res);
- },
- fail: (res) => {
- },
- });
- }
- })
|