mapContent.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. // pages/jsCase/citySel/index.js
  2. const {
  3. cityData
  4. } = require('./city.js')
  5. var map = require('../../../utils/map.js');
  6. var util = require('../../../utils/util.js');
  7. var api = require('../../../config/api.js');
  8. var app = getApp();
  9. Page({
  10. data: {
  11. index: 0,
  12. current_city: "当前定位",
  13. search: "",
  14. hot_city: [{
  15. name: "上海"
  16. }, {
  17. name: "北京"
  18. }, {
  19. name: "深圳"
  20. }, {
  21. name: "南京"
  22. }, {
  23. name: "苏州"
  24. }, {
  25. name: "杭州"
  26. }, {
  27. name: "青岛"
  28. }, {
  29. name: "广州"
  30. }, {
  31. name: "成都"
  32. }, {
  33. name: "重庆"
  34. }, {
  35. name: "昆山"
  36. }, {
  37. name: "武汉"
  38. }, {
  39. name: "西安"
  40. }, {
  41. name: "长沙"
  42. }],
  43. city_list: [],
  44. barHeight: 0,
  45. curr: -1,
  46. scrollViewId: "",
  47. barTop: 0,
  48. showLetter: true,
  49. result: [], //搜索结果
  50. mapShow: true,
  51. resultShow: false,
  52. cityShow: false,
  53. currentMarker: [
  54. // {longitude: 116.40159,
  55. // latitude: 39.90511,
  56. // title: '当前位置',}
  57. ],
  58. lng: '',
  59. lat: '',
  60. poiList: [],
  61. timer: '',
  62. loadTime: 0,
  63. isTouchMap: true
  64. },
  65. onUnload: function () {
  66. // 页面关闭
  67. clearInterval(this.data.timer);
  68. },
  69. onLoad: function (options) {
  70. // this.getUserLocation();
  71. let timer = setInterval(() => {
  72. if (this.data.loadTime < 10) {
  73. this.setData({
  74. loadTime: this.data.loadTime + 1
  75. });
  76. } else {
  77. clearInterval(that.data.timer);
  78. }
  79. }, 1000);
  80. this.setData({
  81. timer: timer
  82. });
  83. this.setData({
  84. city_list: cityData
  85. })
  86. wx.getSystemInfo({
  87. success: (res) => {
  88. console.log(res);
  89. let winHeight = res.windowHeight
  90. let barHeight = winHeight - res.windowWidth / 750 * 300;
  91. this.setData({
  92. barHeight: barHeight,
  93. barTop: res.windowWidth / 750 * 180,
  94. })
  95. }
  96. });
  97. this.mapCtx = wx.createMapContext("myMap");
  98. let that = this;
  99. console.log(options);
  100. if (options.lng && options.lat) {
  101. that.setData({
  102. addressName: options.addressname,
  103. current_city: options.city,
  104. lng: options.lng,
  105. lat: options.lat,
  106. });
  107. that.getPoiList();
  108. } else {
  109. // wx.authorize({
  110. // scope: "scope.userLocation",
  111. // success(res) {
  112. // console.log("已允许授权定位权限: ", res);
  113. wx.getLocation({
  114. type: 'wgs84',
  115. success: function (res) {
  116. let latitude = res.latitude;
  117. let longitude = res.longitude;
  118. app.globalData.lat = latitude; //
  119. app.globalData.lng = longitude; //把onload定位时候的经纬度存到全局
  120. app.globalData.currentLocationName = res.city;
  121. app.globalData.city = res.city;
  122. that.setData({
  123. addressName: app.globalData.currentLocationName,
  124. current_city: app.globalData.city,
  125. lng: app.globalData.lng,
  126. lat: app.globalData.lat,
  127. });
  128. console.log('当前经纬度:' + longitude + ',' + latitude);
  129. that.getPoiList();
  130. },
  131. fail: function (res) {
  132. console.log('fail' + JSON.stringify(res));
  133. }
  134. });
  135. // }
  136. // });
  137. }
  138. },
  139. onReady() {
  140. },
  141. getUserLocation: function () {
  142. map.getUserLocation();
  143. console.log(app.globalData);
  144. let that = this;
  145. setTimeout(() => {
  146. that.setData({
  147. addressName: app.globalData.currentLocationName,
  148. current_city: app.globalData.city,
  149. lng: app.globalData.lng,
  150. lat: app.globalData.lat,
  151. });
  152. this.getPoiList();
  153. }, 1000);
  154. },
  155. /**
  156. * 获取value值
  157. * @param {*} e
  158. */
  159. getValue(e) {
  160. this.setData({
  161. search: e.detail.value
  162. }, () => {
  163. // this.search(e.detail.value);
  164. })
  165. },
  166. confSearch(e){
  167. this.search(this.data.search);
  168. },
  169. /**
  170. * 搜索成功
  171. */
  172. search(e) {
  173. // let result = [], { city_list } = this.data;
  174. // city_list.forEach((item1) => {
  175. // item1.data.forEach((item2) => {
  176. // if (item2.keyword.indexOf(e.toLocaleUpperCase()) !== -1) {
  177. // result.push({ name: item2.cityName })
  178. // }
  179. // })
  180. // })
  181. // this.setData({
  182. // result,
  183. // })
  184. util.request(api.gdSearchUrl, {
  185. keywords: e,
  186. output: 'JSON',
  187. city: this.data.current_city,
  188. key: api.gdKey
  189. }).then(res => {
  190. console.log(res);
  191. if (res.info == 'OK') {
  192. let list = res.tips;
  193. let result = list;
  194. if (list.length > 1) {
  195. // result=list.splice(1);
  196. this.setData({
  197. result: result
  198. });
  199. } else {
  200. this.setData({
  201. result: []
  202. });
  203. }
  204. }
  205. });
  206. },
  207. /**
  208. * 清空验证码
  209. */
  210. clear_input() {
  211. this.setData({
  212. search: "",
  213. mapShow: true,
  214. cityShow: false,
  215. resultShow: false,
  216. })
  217. },
  218. touch(e) {
  219. let pageY = e.touches[0].pageY
  220. let index = Math.floor((pageY - this.data.barTop) / (this.data.barHeight / 22)) //向下取整
  221. let item = this.data.city_list[index]
  222. if (item) {
  223. this.setData({
  224. scrollViewId: item.letter,
  225. curr: index
  226. })
  227. }
  228. },
  229. touchStart(e) {
  230. this.setData({
  231. showLetter: true
  232. })
  233. this.touch(e)
  234. },
  235. touchMove(e) {
  236. this.touch(e)
  237. },
  238. touchEnd() {
  239. this.setData({
  240. showLetter: false,
  241. })
  242. },
  243. touchCancel() {
  244. this.setData({
  245. showLetter: false,
  246. })
  247. },
  248. mapFocus() {
  249. this.setData({
  250. mapShow: false,
  251. resultShow: true,
  252. cityShow: false,
  253. result: []
  254. });
  255. },
  256. switchCity() {
  257. if (this.data.cityShow) {
  258. this.setData({
  259. mapShow: true,
  260. cityShow: false,
  261. resultShow: false
  262. });
  263. } else {
  264. this.setData({
  265. mapShow: false,
  266. cityShow: true,
  267. resultShow: false,
  268. search: ''
  269. });
  270. }
  271. },
  272. msg(title, duration = 1500) {
  273. wx.showToast({
  274. title,
  275. duration,
  276. icon: 'none'
  277. });
  278. },
  279. regionChange(e) {
  280. console.log('isTouchMap', this.data.isTouchMap);
  281. if (this.data.isTouchMap == false) return;
  282. console.log('region变化' + this.data.index + ':' + e.detail.type, e);
  283. // this.msg(this.data.index+'');
  284. // this.msg(`region: latitude: ${ latitude }, longitude: ${ longitude }`);
  285. // 经度范围:73°33′E至135°05′E 东经
  286. // 纬度范围:3°51′N至53°33′N 北纬
  287. //type为end才有centerLocation
  288. // && longitude > 72 && longitude < 136 && latitude > 3 && latitude < 54
  289. if (this.data.loadTime > 3 && e.detail.type == 'end' && e.causedBy == "drag") {
  290. const {
  291. centerLocation
  292. } = e.detail;
  293. let latitude = Number(centerLocation.latitude).toFixed(4);
  294. let longitude = Number(centerLocation.longitude).toFixed(4);
  295. console.log('当前中心点经纬度', latitude, longitude);
  296. // if (latitude == this.data.lat) return;
  297. this.setData({
  298. lng: longitude,
  299. lat: latitude,
  300. index: this.data.index + 1
  301. });
  302. this.getPoiList();
  303. // }
  304. }
  305. // this.setData({
  306. // currentMarker:[
  307. // {longitude:Number(longitude),
  308. // latitude:Number(latitude),
  309. // // title: '',
  310. // }
  311. // ]
  312. // });
  313. },
  314. mapTap(e) {
  315. console.log(e.detail);
  316. },
  317. getPoiList() {
  318. util.request(api.gdUrl, {
  319. // location:'116.40159,39.90511',
  320. location: this.data.lng + ',' + this.data.lat,
  321. extensions: 'all',
  322. // poitype:'',
  323. key: api.gdKey,
  324. radius: 3000,
  325. }).then(res => {
  326. console.log('poi', res);
  327. if (res.info == 'OK') {
  328. let list = res.regeocode.pois;
  329. var t = '';
  330. var length = list.length;
  331. for (var i = 0; i < length; i++) {
  332. for (var j = 0; j < length - i - 1; j++) {
  333. if (Number(list[j].distance) > Number(list[j + 1].distance)) {
  334. t = list[j];
  335. list[j] = list[j + 1];
  336. list[j + 1] = t;
  337. }
  338. }
  339. }
  340. this.setData({
  341. currentName: list.length > 0 ? list[0].name : res.regeocode.addressComponent.streetNumber.street,
  342. poiList: list,
  343. })
  344. }
  345. });
  346. },
  347. resultTap(e) {
  348. // adcode: "110101"
  349. // address: "1号线/八通线"
  350. // city: []
  351. // district: "北京市东城区"
  352. // id: "BV10006499"
  353. // location: "116.401216,39.907780"
  354. // name: "天安门东(地铁站)"
  355. // typecode: "150500"
  356. let location = e.currentTarget.dataset.location;
  357. let name = e.currentTarget.dataset.name;
  358. this.setData({
  359. currentName: name,
  360. lng: location.split(',')[0],
  361. lat: location.split(',')[1],
  362. resultShow: false,
  363. mapShow: true,
  364. });
  365. this.getPoiList();
  366. },
  367. cityTap(e) {
  368. // this.setData({
  369. // isTouchMap: false
  370. // });
  371. console.log('isTouchMap', this.data.isTouchMap);
  372. let city = e.currentTarget.dataset.city;
  373. console.log(city);
  374. util.request(api.gdCityUrl, {
  375. address: city,
  376. output: 'JSON',
  377. key: api.gdKey,
  378. }).then(res => {
  379. console.log(res);
  380. if (res.info == 'OK') {
  381. let location = res.geocodes[0].location;
  382. this.setData({
  383. current_city: city,
  384. lng: location.split(',')[0],
  385. lat: location.split(',')[1],
  386. cityShow: false,
  387. mapShow: true,
  388. });
  389. this.getPoiList();
  390. // setTimeout(() => {
  391. // this.setData({
  392. // isTouchMap: true
  393. // })
  394. // }, 500);
  395. }
  396. });
  397. },
  398. poiTap(e) {
  399. // address: "菖蒲河沿9号(天安门东地铁站B东北口步行200米)"
  400. // businessarea: "东单"
  401. // direction: "东"
  402. // distance: "210.116"
  403. // id: "B0FFFLJW2Q"
  404. // location: "116.403456,39.908566"
  405. // name: "菖蒲河公园"
  406. // poiweight: "0.426156"
  407. // tel: []
  408. // type: "风景名胜;公园广场;公园"
  409. let poi = e.currentTarget.dataset.poi;
  410. console.log(poi);
  411. app.globalData.mapSelectFinish = true;
  412. app.globalData.mapSelectPoi = poi;
  413. wx.navigateBack();
  414. },
  415. mapUpdated(e) {
  416. console.log('mapupdated');
  417. console.log(e);
  418. // this.mapCtx = wx.createMapContext("myMap");
  419. this.mapCtx.getCenterLocation({
  420. success: (res) => {
  421. console.log(res);
  422. },
  423. fail: (res) => {
  424. },
  425. });
  426. }
  427. })