qqmap-wx-jssdk.js 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123
  1. /**
  2. * 微信小程序JavaScriptSDK
  3. *
  4. * @version 1.2
  5. * @date 2019-03-06
  6. * @author v_ylyue@tencent.com
  7. */
  8. var ERROR_CONF = {
  9. KEY_ERR: 311,
  10. KEY_ERR_MSG: 'key格式错误',
  11. PARAM_ERR: 310,
  12. PARAM_ERR_MSG: '请求参数信息有误',
  13. SYSTEM_ERR: 600,
  14. SYSTEM_ERR_MSG: '系统错误',
  15. WX_ERR_CODE: 1000,
  16. WX_OK_CODE: 200
  17. };
  18. var BASE_URL = 'https://apis.map.qq.com/ws/';
  19. var URL_SEARCH = BASE_URL + 'place/v1/search';
  20. var URL_SUGGESTION = BASE_URL + 'place/v1/suggestion';
  21. var URL_GET_GEOCODER = BASE_URL + 'geocoder/v1/';
  22. var URL_CITY_LIST = BASE_URL + 'district/v1/list';
  23. var URL_AREA_LIST = BASE_URL + 'district/v1/getchildren';
  24. var URL_DISTANCE = BASE_URL + 'distance/v1/';
  25. var URL_DIRECTION = BASE_URL + 'direction/v1/';
  26. var MODE = {
  27. driving: 'driving',
  28. transit: 'transit'
  29. };
  30. var EARTH_RADIUS = 6378136.49;
  31. var Utils = {
  32. /**
  33. * md5加密方法
  34. * 版权所有©2011 Sebastian Tschan,https://blueimp.net
  35. */
  36. safeAdd(x, y) {
  37. var lsw = (x & 0xffff) + (y & 0xffff);
  38. var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
  39. return (msw << 16) | (lsw & 0xffff);
  40. },
  41. bitRotateLeft(num, cnt) {
  42. return (num << cnt) | (num >>> (32 - cnt));
  43. },
  44. md5cmn(q, a, b, x, s, t) {
  45. return this.safeAdd(this.bitRotateLeft(this.safeAdd(this.safeAdd(a, q), this.safeAdd(x, t)), s), b);
  46. },
  47. md5ff(a, b, c, d, x, s, t) {
  48. return this.md5cmn((b & c) | (~b & d), a, b, x, s, t);
  49. },
  50. md5gg(a, b, c, d, x, s, t) {
  51. return this.md5cmn((b & d) | (c & ~d), a, b, x, s, t);
  52. },
  53. md5hh(a, b, c, d, x, s, t) {
  54. return this.md5cmn(b ^ c ^ d, a, b, x, s, t);
  55. },
  56. md5ii(a, b, c, d, x, s, t) {
  57. return this.md5cmn(c ^ (b | ~d), a, b, x, s, t);
  58. },
  59. binlMD5(x, len) {
  60. /* append padding */
  61. x[len >> 5] |= 0x80 << (len % 32);
  62. x[((len + 64) >>> 9 << 4) + 14] = len;
  63. var i;
  64. var olda;
  65. var oldb;
  66. var oldc;
  67. var oldd;
  68. var a = 1732584193;
  69. var b = -271733879;
  70. var c = -1732584194;
  71. var d = 271733878;
  72. for (i = 0; i < x.length; i += 16) {
  73. olda = a;
  74. oldb = b;
  75. oldc = c;
  76. oldd = d;
  77. a = this.md5ff(a, b, c, d, x[i], 7, -680876936);
  78. d = this.md5ff(d, a, b, c, x[i + 1], 12, -389564586);
  79. c = this.md5ff(c, d, a, b, x[i + 2], 17, 606105819);
  80. b = this.md5ff(b, c, d, a, x[i + 3], 22, -1044525330);
  81. a = this.md5ff(a, b, c, d, x[i + 4], 7, -176418897);
  82. d = this.md5ff(d, a, b, c, x[i + 5], 12, 1200080426);
  83. c = this.md5ff(c, d, a, b, x[i + 6], 17, -1473231341);
  84. b = this.md5ff(b, c, d, a, x[i + 7], 22, -45705983);
  85. a = this.md5ff(a, b, c, d, x[i + 8], 7, 1770035416);
  86. d = this.md5ff(d, a, b, c, x[i + 9], 12, -1958414417);
  87. c = this.md5ff(c, d, a, b, x[i + 10], 17, -42063);
  88. b = this.md5ff(b, c, d, a, x[i + 11], 22, -1990404162);
  89. a = this.md5ff(a, b, c, d, x[i + 12], 7, 1804603682);
  90. d = this.md5ff(d, a, b, c, x[i + 13], 12, -40341101);
  91. c = this.md5ff(c, d, a, b, x[i + 14], 17, -1502002290);
  92. b = this.md5ff(b, c, d, a, x[i + 15], 22, 1236535329);
  93. a = this.md5gg(a, b, c, d, x[i + 1], 5, -165796510);
  94. d = this.md5gg(d, a, b, c, x[i + 6], 9, -1069501632);
  95. c = this.md5gg(c, d, a, b, x[i + 11], 14, 643717713);
  96. b = this.md5gg(b, c, d, a, x[i], 20, -373897302);
  97. a = this.md5gg(a, b, c, d, x[i + 5], 5, -701558691);
  98. d = this.md5gg(d, a, b, c, x[i + 10], 9, 38016083);
  99. c = this.md5gg(c, d, a, b, x[i + 15], 14, -660478335);
  100. b = this.md5gg(b, c, d, a, x[i + 4], 20, -405537848);
  101. a = this.md5gg(a, b, c, d, x[i + 9], 5, 568446438);
  102. d = this.md5gg(d, a, b, c, x[i + 14], 9, -1019803690);
  103. c = this.md5gg(c, d, a, b, x[i + 3], 14, -187363961);
  104. b = this.md5gg(b, c, d, a, x[i + 8], 20, 1163531501);
  105. a = this.md5gg(a, b, c, d, x[i + 13], 5, -1444681467);
  106. d = this.md5gg(d, a, b, c, x[i + 2], 9, -51403784);
  107. c = this.md5gg(c, d, a, b, x[i + 7], 14, 1735328473);
  108. b = this.md5gg(b, c, d, a, x[i + 12], 20, -1926607734);
  109. a = this.md5hh(a, b, c, d, x[i + 5], 4, -378558);
  110. d = this.md5hh(d, a, b, c, x[i + 8], 11, -2022574463);
  111. c = this.md5hh(c, d, a, b, x[i + 11], 16, 1839030562);
  112. b = this.md5hh(b, c, d, a, x[i + 14], 23, -35309556);
  113. a = this.md5hh(a, b, c, d, x[i + 1], 4, -1530992060);
  114. d = this.md5hh(d, a, b, c, x[i + 4], 11, 1272893353);
  115. c = this.md5hh(c, d, a, b, x[i + 7], 16, -155497632);
  116. b = this.md5hh(b, c, d, a, x[i + 10], 23, -1094730640);
  117. a = this.md5hh(a, b, c, d, x[i + 13], 4, 681279174);
  118. d = this.md5hh(d, a, b, c, x[i], 11, -358537222);
  119. c = this.md5hh(c, d, a, b, x[i + 3], 16, -722521979);
  120. b = this.md5hh(b, c, d, a, x[i + 6], 23, 76029189);
  121. a = this.md5hh(a, b, c, d, x[i + 9], 4, -640364487);
  122. d = this.md5hh(d, a, b, c, x[i + 12], 11, -421815835);
  123. c = this.md5hh(c, d, a, b, x[i + 15], 16, 530742520);
  124. b = this.md5hh(b, c, d, a, x[i + 2], 23, -995338651);
  125. a = this.md5ii(a, b, c, d, x[i], 6, -198630844);
  126. d = this.md5ii(d, a, b, c, x[i + 7], 10, 1126891415);
  127. c = this.md5ii(c, d, a, b, x[i + 14], 15, -1416354905);
  128. b = this.md5ii(b, c, d, a, x[i + 5], 21, -57434055);
  129. a = this.md5ii(a, b, c, d, x[i + 12], 6, 1700485571);
  130. d = this.md5ii(d, a, b, c, x[i + 3], 10, -1894986606);
  131. c = this.md5ii(c, d, a, b, x[i + 10], 15, -1051523);
  132. b = this.md5ii(b, c, d, a, x[i + 1], 21, -2054922799);
  133. a = this.md5ii(a, b, c, d, x[i + 8], 6, 1873313359);
  134. d = this.md5ii(d, a, b, c, x[i + 15], 10, -30611744);
  135. c = this.md5ii(c, d, a, b, x[i + 6], 15, -1560198380);
  136. b = this.md5ii(b, c, d, a, x[i + 13], 21, 1309151649);
  137. a = this.md5ii(a, b, c, d, x[i + 4], 6, -145523070);
  138. d = this.md5ii(d, a, b, c, x[i + 11], 10, -1120210379);
  139. c = this.md5ii(c, d, a, b, x[i + 2], 15, 718787259);
  140. b = this.md5ii(b, c, d, a, x[i + 9], 21, -343485551);
  141. a = this.safeAdd(a, olda);
  142. b = this.safeAdd(b, oldb);
  143. c = this.safeAdd(c, oldc);
  144. d = this.safeAdd(d, oldd);
  145. }
  146. return [a, b, c, d];
  147. },
  148. binl2rstr(input) {
  149. var i;
  150. var output = '';
  151. var length32 = input.length * 32;
  152. for (i = 0; i < length32; i += 8) {
  153. output += String.fromCharCode((input[i >> 5] >>> (i % 32)) & 0xff);
  154. }
  155. return output;
  156. },
  157. rstr2binl(input) {
  158. var i;
  159. var output = [];
  160. output[(input.length >> 2) - 1] = undefined;
  161. for (i = 0; i < output.length; i += 1) {
  162. output[i] = 0;
  163. }
  164. var length8 = input.length * 8;
  165. for (i = 0; i < length8; i += 8) {
  166. output[i >> 5] |= (input.charCodeAt(i / 8) & 0xff) << (i % 32);
  167. }
  168. return output;
  169. },
  170. rstrMD5(s) {
  171. return this.binl2rstr(this.binlMD5(this.rstr2binl(s), s.length * 8));
  172. },
  173. rstrHMACMD5(key, data) {
  174. var i;
  175. var bkey = this.rstr2binl(key);
  176. var ipad = [];
  177. var opad = [];
  178. var hash;
  179. ipad[15] = opad[15] = undefined;
  180. if (bkey.length > 16) {
  181. bkey = this.binlMD5(bkey, key.length * 8);
  182. }
  183. for (i = 0; i < 16; i += 1) {
  184. ipad[i] = bkey[i] ^ 0x36363636;
  185. opad[i] = bkey[i] ^ 0x5c5c5c5c;
  186. }
  187. hash = this.binlMD5(ipad.concat(this.rstr2binl(data)), 512 + data.length * 8);
  188. return this.binl2rstr(this.binlMD5(opad.concat(hash), 512 + 128));
  189. },
  190. rstr2hex(input) {
  191. var hexTab = '0123456789abcdef';
  192. var output = '';
  193. var x;
  194. var i;
  195. for (i = 0; i < input.length; i += 1) {
  196. x = input.charCodeAt(i);
  197. output += hexTab.charAt((x >>> 4) & 0x0f) + hexTab.charAt(x & 0x0f);
  198. }
  199. return output;
  200. },
  201. str2rstrUTF8(input) {
  202. return unescape(encodeURIComponent(input));
  203. },
  204. rawMD5(s) {
  205. return this.rstrMD5(this.str2rstrUTF8(s));
  206. },
  207. hexMD5(s) {
  208. return this.rstr2hex(this.rawMD5(s));
  209. },
  210. rawHMACMD5(k, d) {
  211. return this.rstrHMACMD5(this.str2rstrUTF8(k), str2rstrUTF8(d));
  212. },
  213. hexHMACMD5(k, d) {
  214. return this.rstr2hex(this.rawHMACMD5(k, d));
  215. },
  216. md5(string, key, raw) {
  217. if (!key) {
  218. if (!raw) {
  219. return this.hexMD5(string);
  220. }
  221. return this.rawMD5(string);
  222. }
  223. if (!raw) {
  224. return this.hexHMACMD5(key, string);
  225. }
  226. return this.rawHMACMD5(key, string);
  227. },
  228. /**
  229. * 得到md5加密后的sig参数
  230. * @param {Object} requestParam 接口参数
  231. * @param {String} sk签名字符串
  232. * @param {String} featrue 方法名
  233. * @return 返回加密后的sig参数
  234. */
  235. getSig(requestParam, sk, feature, mode) {
  236. var sig = null;
  237. var requestArr = [];
  238. Object.keys(requestParam).sort().forEach(function(key){
  239. requestArr.push(key + '=' + requestParam[key]);
  240. });
  241. if (feature == 'search') {
  242. sig = '/ws/place/v1/search?' + requestArr.join('&') + sk;
  243. }
  244. if (feature == 'suggest') {
  245. sig = '/ws/place/v1/suggestion?' + requestArr.join('&') + sk;
  246. }
  247. if (feature == 'reverseGeocoder') {
  248. sig = '/ws/geocoder/v1/?' + requestArr.join('&') + sk;
  249. }
  250. if (feature == 'geocoder') {
  251. sig = '/ws/geocoder/v1/?' + requestArr.join('&') + sk;
  252. }
  253. if (feature == 'getCityList') {
  254. sig = '/ws/district/v1/list?' + requestArr.join('&') + sk;
  255. }
  256. if (feature == 'getDistrictByCityId') {
  257. sig = '/ws/district/v1/getchildren?' + requestArr.join('&') + sk;
  258. }
  259. if (feature == 'calculateDistance') {
  260. sig = '/ws/distance/v1/?' + requestArr.join('&') + sk;
  261. }
  262. if (feature == 'direction') {
  263. sig = '/ws/direction/v1/' + mode + '?' + requestArr.join('&') + sk;
  264. }
  265. sig = this.md5(sig);
  266. return sig;
  267. },
  268. /**
  269. * 得到终点query字符串
  270. * @param {Array|String} 检索数据
  271. */
  272. location2query(data) {
  273. if (typeof data == 'string') {
  274. return data;
  275. }
  276. var query = '';
  277. for (var i = 0; i < data.length; i++) {
  278. var d = data[i];
  279. if (!!query) {
  280. query += ';';
  281. }
  282. if (d.location) {
  283. query = query + d.location.lat + ',' + d.location.lng;
  284. }
  285. if (d.latitude && d.longitude) {
  286. query = query + d.latitude + ',' + d.longitude;
  287. }
  288. }
  289. return query;
  290. },
  291. /**
  292. * 计算角度
  293. */
  294. rad(d) {
  295. return d * Math.PI / 180.0;
  296. },
  297. /**
  298. * 处理终点location数组
  299. * @return 返回终点数组
  300. */
  301. getEndLocation(location){
  302. var to = location.split(';');
  303. var endLocation = [];
  304. for (var i = 0; i < to.length; i++) {
  305. endLocation.push({
  306. lat: parseFloat(to[i].split(',')[0]),
  307. lng: parseFloat(to[i].split(',')[1])
  308. })
  309. }
  310. return endLocation;
  311. },
  312. /**
  313. * 计算两点间直线距离
  314. * @param a 表示纬度差
  315. * @param b 表示经度差
  316. * @return 返回的是距离,单位m
  317. */
  318. getDistance(latFrom, lngFrom, latTo, lngTo) {
  319. var radLatFrom = this.rad(latFrom);
  320. var radLatTo = this.rad(latTo);
  321. var a = radLatFrom - radLatTo;
  322. var b = this.rad(lngFrom) - this.rad(lngTo);
  323. var distance = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLatFrom) * Math.cos(radLatTo) * Math.pow(Math.sin(b / 2), 2)));
  324. distance = distance * EARTH_RADIUS;
  325. distance = Math.round(distance * 10000) / 10000;
  326. return parseFloat(distance.toFixed(0));
  327. },
  328. /**
  329. * 使用微信接口进行定位
  330. */
  331. getWXLocation(success, fail, complete) {
  332. wx.getLocation({
  333. type: 'gcj02',
  334. success: success,
  335. fail: fail,
  336. complete: complete
  337. });
  338. },
  339. /**
  340. * 获取location参数
  341. */
  342. getLocationParam(location) {
  343. if (typeof location == 'string') {
  344. var locationArr = location.split(',');
  345. if (locationArr.length === 2) {
  346. location = {
  347. latitude: location.split(',')[0],
  348. longitude: location.split(',')[1]
  349. };
  350. } else {
  351. location = {};
  352. }
  353. }
  354. return location;
  355. },
  356. /**
  357. * 回调函数默认处理
  358. */
  359. polyfillParam(param) {
  360. param.success = param.success || function () { };
  361. param.fail = param.fail || function () { };
  362. param.complete = param.complete || function () { };
  363. },
  364. /**
  365. * 验证param对应的key值是否为空
  366. *
  367. * @param {Object} param 接口参数
  368. * @param {String} key 对应参数的key
  369. */
  370. checkParamKeyEmpty(param, key) {
  371. if (!param[key]) {
  372. var errconf = this.buildErrorConfig(ERROR_CONF.PARAM_ERR, ERROR_CONF.PARAM_ERR_MSG + key +'参数格式有误');
  373. param.fail(errconf);
  374. param.complete(errconf);
  375. return true;
  376. }
  377. return false;
  378. },
  379. /**
  380. * 验证参数中是否存在检索词keyword
  381. *
  382. * @param {Object} param 接口参数
  383. */
  384. checkKeyword(param){
  385. return !this.checkParamKeyEmpty(param, 'keyword');
  386. },
  387. /**
  388. * 验证location值
  389. *
  390. * @param {Object} param 接口参数
  391. */
  392. checkLocation(param) {
  393. var location = this.getLocationParam(param.location);
  394. if (!location || !location.latitude || !location.longitude) {
  395. var errconf = this.buildErrorConfig(ERROR_CONF.PARAM_ERR, ERROR_CONF.PARAM_ERR_MSG + ' location参数格式有误');
  396. param.fail(errconf);
  397. param.complete(errconf);
  398. return false;
  399. }
  400. return true;
  401. },
  402. /**
  403. * 构造错误数据结构
  404. * @param {Number} errCode 错误码
  405. * @param {Number} errMsg 错误描述
  406. */
  407. buildErrorConfig(errCode, errMsg) {
  408. return {
  409. status: errCode,
  410. message: errMsg
  411. };
  412. },
  413. /**
  414. *
  415. * 数据处理函数
  416. * 根据传入参数不同处理不同数据
  417. * @param {String} feature 功能名称
  418. * search 地点搜索
  419. * suggest关键词提示
  420. * reverseGeocoder逆地址解析
  421. * geocoder地址解析
  422. * getCityList获取城市列表:父集
  423. * getDistrictByCityId获取区县列表:子集
  424. * calculateDistance距离计算
  425. * @param {Object} param 接口参数
  426. * @param {Object} data 数据
  427. */
  428. handleData(param,data,feature){
  429. if (feature == 'search') {
  430. var searchResult = data.data;
  431. var searchSimplify = [];
  432. for (var i = 0; i < searchResult.length; i++) {
  433. searchSimplify.push({
  434. id: searchResult[i].id || null,
  435. title: searchResult[i].title || null,
  436. latitude: searchResult[i].location && searchResult[i].location.lat || null,
  437. longitude: searchResult[i].location && searchResult[i].location.lng || null,
  438. address: searchResult[i].address || null,
  439. category: searchResult[i].category || null,
  440. tel: searchResult[i].tel || null,
  441. adcode: searchResult[i].ad_info && searchResult[i].ad_info.adcode || null,
  442. city: searchResult[i].ad_info && searchResult[i].ad_info.city || null,
  443. district: searchResult[i].ad_info && searchResult[i].ad_info.district || null,
  444. province: searchResult[i].ad_info && searchResult[i].ad_info.province || null
  445. })
  446. }
  447. param.success(data, {
  448. searchResult: searchResult,
  449. searchSimplify: searchSimplify
  450. })
  451. } else if (feature == 'suggest') {
  452. var suggestResult = data.data;
  453. var suggestSimplify = [];
  454. for (var i = 0; i < suggestResult.length; i++) {
  455. suggestSimplify.push({
  456. adcode: suggestResult[i].adcode || null,
  457. address: suggestResult[i].address || null,
  458. category: suggestResult[i].category || null,
  459. city: suggestResult[i].city || null,
  460. district: suggestResult[i].district || null,
  461. id: suggestResult[i].id || null,
  462. latitude: suggestResult[i].location && suggestResult[i].location.lat || null,
  463. longitude: suggestResult[i].location && suggestResult[i].location.lng || null,
  464. province: suggestResult[i].province || null,
  465. title: suggestResult[i].title || null,
  466. type: suggestResult[i].type || null
  467. })
  468. }
  469. param.success(data, {
  470. suggestResult: suggestResult,
  471. suggestSimplify: suggestSimplify
  472. })
  473. } else if (feature == 'reverseGeocoder') {
  474. var reverseGeocoderResult = data.result;
  475. var reverseGeocoderSimplify = {
  476. address: reverseGeocoderResult.address || null,
  477. latitude: reverseGeocoderResult.location && reverseGeocoderResult.location.lat || null,
  478. longitude: reverseGeocoderResult.location && reverseGeocoderResult.location.lng || null,
  479. adcode: reverseGeocoderResult.ad_info && reverseGeocoderResult.ad_info.adcode || null,
  480. city: reverseGeocoderResult.address_component && reverseGeocoderResult.address_component.city || null,
  481. district: reverseGeocoderResult.address_component && reverseGeocoderResult.address_component.district || null,
  482. nation: reverseGeocoderResult.address_component && reverseGeocoderResult.address_component.nation || null,
  483. province: reverseGeocoderResult.address_component && reverseGeocoderResult.address_component.province || null,
  484. street: reverseGeocoderResult.address_component && reverseGeocoderResult.address_component.street || null,
  485. street_number: reverseGeocoderResult.address_component && reverseGeocoderResult.address_component.street_number || null,
  486. recommend: reverseGeocoderResult.formatted_addresses && reverseGeocoderResult.formatted_addresses.recommend || null,
  487. rough: reverseGeocoderResult.formatted_addresses && reverseGeocoderResult.formatted_addresses.rough || null
  488. };
  489. if (reverseGeocoderResult.pois) {//判断是否返回周边poi
  490. var pois = reverseGeocoderResult.pois;
  491. var poisSimplify = [];
  492. for (var i = 0;i < pois.length;i++) {
  493. poisSimplify.push({
  494. id: pois[i].id || null,
  495. title: pois[i].title || null,
  496. latitude: pois[i].location && pois[i].location.lat || null,
  497. longitude: pois[i].location && pois[i].location.lng || null,
  498. address: pois[i].address || null,
  499. category: pois[i].category || null,
  500. adcode: pois[i].ad_info && pois[i].ad_info.adcode || null,
  501. city: pois[i].ad_info && pois[i].ad_info.city || null,
  502. district: pois[i].ad_info && pois[i].ad_info.district || null,
  503. province: pois[i].ad_info && pois[i].ad_info.province || null
  504. })
  505. }
  506. param.success(data,{
  507. reverseGeocoderResult: reverseGeocoderResult,
  508. reverseGeocoderSimplify: reverseGeocoderSimplify,
  509. pois: pois,
  510. poisSimplify: poisSimplify
  511. })
  512. } else {
  513. param.success(data, {
  514. reverseGeocoderResult: reverseGeocoderResult,
  515. reverseGeocoderSimplify: reverseGeocoderSimplify
  516. })
  517. }
  518. } else if (feature == 'geocoder') {
  519. var geocoderResult = data.result;
  520. var geocoderSimplify = {
  521. title: geocoderResult.title || null,
  522. latitude: geocoderResult.location && geocoderResult.location.lat || null,
  523. longitude: geocoderResult.location && geocoderResult.location.lng || null,
  524. adcode: geocoderResult.ad_info && geocoderResult.ad_info.adcode || null,
  525. province: geocoderResult.address_components && geocoderResult.address_components.province || null,
  526. city: geocoderResult.address_components && geocoderResult.address_components.city || null,
  527. district: geocoderResult.address_components && geocoderResult.address_components.district || null,
  528. street: geocoderResult.address_components && geocoderResult.address_components.street || null,
  529. street_number: geocoderResult.address_components && geocoderResult.address_components.street_number || null,
  530. level: geocoderResult.level || null
  531. };
  532. param.success(data,{
  533. geocoderResult: geocoderResult,
  534. geocoderSimplify: geocoderSimplify
  535. });
  536. } else if (feature == 'getCityList') {
  537. var provinceResult = data.result[0];
  538. var cityResult = data.result[1];
  539. var districtResult = data.result[2];
  540. param.success(data,{
  541. provinceResult: provinceResult,
  542. cityResult: cityResult,
  543. districtResult: districtResult
  544. });
  545. } else if (feature == 'getDistrictByCityId') {
  546. var districtByCity = data.result[0];
  547. param.success(data, districtByCity);
  548. } else if (feature == 'calculateDistance') {
  549. var calculateDistanceResult = data.result.elements;
  550. var distance = [];
  551. for (var i = 0; i < calculateDistanceResult.length; i++){
  552. distance.push(calculateDistanceResult[i].distance);
  553. }
  554. param.success(data, {
  555. calculateDistanceResult: calculateDistanceResult,
  556. distance: distance
  557. });
  558. } else if (feature == 'direction') {
  559. var direction = data.result.routes;
  560. param.success(data,direction);
  561. } else {
  562. param.success(data);
  563. }
  564. },
  565. /**
  566. * 构造微信请求参数,公共属性处理
  567. *
  568. * @param {Object} param 接口参数
  569. * @param {Object} param 配置项
  570. * @param {String} feature 方法名
  571. */
  572. buildWxRequestConfig(param, options, feature) {
  573. var that = this;
  574. options.header = { "content-type": "application/json" };
  575. options.method = 'GET';
  576. options.success = function (res) {
  577. var data = res.data;
  578. if (data.status === 0) {
  579. that.handleData(param, data, feature);
  580. } else {
  581. param.fail(data);
  582. }
  583. };
  584. options.fail = function (res) {
  585. res.statusCode = ERROR_CONF.WX_ERR_CODE;
  586. param.fail(that.buildErrorConfig(ERROR_CONF.WX_ERR_CODE, res.errMsg));
  587. };
  588. options.complete = function (res) {
  589. var statusCode = +res.statusCode;
  590. switch(statusCode) {
  591. case ERROR_CONF.WX_ERR_CODE: {
  592. param.complete(that.buildErrorConfig(ERROR_CONF.WX_ERR_CODE, res.errMsg));
  593. break;
  594. }
  595. case ERROR_CONF.WX_OK_CODE: {
  596. var data = res.data;
  597. if (data.status === 0) {
  598. param.complete(data);
  599. } else {
  600. param.complete(that.buildErrorConfig(data.status, data.message));
  601. }
  602. break;
  603. }
  604. default:{
  605. param.complete(that.buildErrorConfig(ERROR_CONF.SYSTEM_ERR, ERROR_CONF.SYSTEM_ERR_MSG));
  606. }
  607. }
  608. };
  609. return options;
  610. },
  611. /**
  612. * 处理用户参数是否传入坐标进行不同的处理
  613. */
  614. locationProcess(param, locationsuccess, locationfail, locationcomplete) {
  615. var that = this;
  616. locationfail = locationfail || function (res) {
  617. res.statusCode = ERROR_CONF.WX_ERR_CODE;
  618. param.fail(that.buildErrorConfig(ERROR_CONF.WX_ERR_CODE, res.errMsg));
  619. };
  620. locationcomplete = locationcomplete || function (res) {
  621. if (res.statusCode == ERROR_CONF.WX_ERR_CODE) {
  622. param.complete(that.buildErrorConfig(ERROR_CONF.WX_ERR_CODE, res.errMsg));
  623. }
  624. };
  625. if (!param.location) {
  626. that.getWXLocation(locationsuccess, locationfail, locationcomplete);
  627. } else if (that.checkLocation(param)) {
  628. var location = Utils.getLocationParam(param.location);
  629. locationsuccess(location);
  630. }
  631. }
  632. };
  633. class QQMapWX {
  634. /**
  635. * 构造函数
  636. *
  637. * @param {Object} options 接口参数,key 为必选参数
  638. */
  639. constructor(options) {
  640. if (!options.key) {
  641. throw Error('key值不能为空');
  642. }
  643. this.key = options.key;
  644. };
  645. /**
  646. * POI周边检索
  647. *
  648. * @param {Object} options 接口参数对象
  649. *
  650. * 参数对象结构可以参考
  651. * @see http://lbs.qq.com/webservice_v1/guide-search.html
  652. */
  653. search(options) {
  654. var that = this;
  655. options = options || {};
  656. Utils.polyfillParam(options);
  657. if (!Utils.checkKeyword(options)) {
  658. return;
  659. }
  660. var requestParam = {
  661. keyword: options.keyword,
  662. orderby: options.orderby || '_distance',
  663. page_size: options.page_size || 10,
  664. page_index: options.page_index || 1,
  665. output: 'json',
  666. key: that.key
  667. };
  668. if (options.address_format) {
  669. requestParam.address_format = options.address_format;
  670. }
  671. if (options.filter) {
  672. requestParam.filter = options.filter;
  673. }
  674. var distance = options.distance || "1000";
  675. var auto_extend = options.auto_extend || 1;
  676. var region = null;
  677. var rectangle = null;
  678. //判断城市限定参数
  679. if (options.region) {
  680. region = options.region;
  681. }
  682. //矩形限定坐标(暂时只支持字符串格式)
  683. if (options.rectangle) {
  684. rectangle = options.rectangle;
  685. }
  686. var locationsuccess = function (result) {
  687. if (region && !rectangle) {
  688. //城市限定参数拼接
  689. requestParam.boundary = "region(" + region + "," + auto_extend + "," + result.latitude + "," + result.longitude + ")";
  690. if (options.sig) {
  691. requestParam.sig = Utils.getSig(requestParam, options.sig, 'search');
  692. }
  693. } else if (rectangle && !region) {
  694. //矩形搜索
  695. requestParam.boundary = "rectangle(" + rectangle + ")";
  696. if (options.sig) {
  697. requestParam.sig = Utils.getSig(requestParam, options.sig, 'search');
  698. }
  699. } else {
  700. requestParam.boundary = "nearby(" + result.latitude + "," + result.longitude + "," + distance + "," + auto_extend + ")";
  701. if (options.sig) {
  702. requestParam.sig = Utils.getSig(requestParam, options.sig, 'search');
  703. }
  704. }
  705. wx.request(Utils.buildWxRequestConfig(options, {
  706. url: URL_SEARCH,
  707. data: requestParam
  708. }, 'search'));
  709. };
  710. Utils.locationProcess(options, locationsuccess);
  711. };
  712. /**
  713. * sug模糊检索
  714. *
  715. * @param {Object} options 接口参数对象
  716. *
  717. * 参数对象结构可以参考
  718. * http://lbs.qq.com/webservice_v1/guide-suggestion.html
  719. */
  720. getSuggestion(options) {
  721. var that = this;
  722. options = options || {};
  723. Utils.polyfillParam(options);
  724. if (!Utils.checkKeyword(options)) {
  725. return;
  726. }
  727. var requestParam = {
  728. keyword: options.keyword,
  729. region: options.region || '全国',
  730. region_fix: options.region_fix || 0,
  731. policy: options.policy || 0,
  732. page_size: options.page_size || 10,//控制显示条数
  733. page_index: options.page_index || 1,//控制页数
  734. get_subpois : options.get_subpois || 0,//返回子地点
  735. output: 'json',
  736. key: that.key
  737. };
  738. //长地址
  739. if (options.address_format) {
  740. requestParam.address_format = options.address_format;
  741. }
  742. //过滤
  743. if (options.filter) {
  744. requestParam.filter = options.filter;
  745. }
  746. //排序
  747. if (options.location) {
  748. var locationsuccess = function (result) {
  749. requestParam.location = result.latitude + ',' + result.longitude;
  750. if (options.sig) {
  751. requestParam.sig = Utils.getSig(requestParam, options.sig, 'suggest');
  752. }
  753. wx.request(Utils.buildWxRequestConfig(options, {
  754. url: URL_SUGGESTION,
  755. data: requestParam
  756. }, "suggest"));
  757. };
  758. Utils.locationProcess(options, locationsuccess);
  759. } else {
  760. if (options.sig) {
  761. requestParam.sig = Utils.getSig(requestParam, options.sig, 'suggest');
  762. }
  763. wx.request(Utils.buildWxRequestConfig(options, {
  764. url: URL_SUGGESTION,
  765. data: requestParam
  766. }, "suggest"));
  767. }
  768. };
  769. /**
  770. * 逆地址解析
  771. *
  772. * @param {Object} options 接口参数对象
  773. *
  774. * 请求参数结构可以参考
  775. * http://lbs.qq.com/webservice_v1/guide-gcoder.html
  776. */
  777. reverseGeocoder(options) {
  778. var that = this;
  779. options = options || {};
  780. Utils.polyfillParam(options);
  781. var requestParam = {
  782. coord_type: options.coord_type || 5,
  783. get_poi: options.get_poi || 0,
  784. output: 'json',
  785. key: that.key
  786. };
  787. if (options.poi_options) {
  788. requestParam.poi_options = options.poi_options
  789. }
  790. var locationsuccess = function (result) {
  791. requestParam.location = result.latitude + ',' + result.longitude;
  792. if (options.sig) {
  793. requestParam.sig = Utils.getSig(requestParam, options.sig, 'reverseGeocoder');
  794. }
  795. wx.request(Utils.buildWxRequestConfig(options, {
  796. url: URL_GET_GEOCODER,
  797. data: requestParam
  798. }, 'reverseGeocoder'));
  799. };
  800. Utils.locationProcess(options, locationsuccess);
  801. };
  802. /**
  803. * 地址解析
  804. *
  805. * @param {Object} options 接口参数对象
  806. *
  807. * 请求参数结构可以参考
  808. * http://lbs.qq.com/webservice_v1/guide-geocoder.html
  809. */
  810. geocoder(options) {
  811. var that = this;
  812. options = options || {};
  813. Utils.polyfillParam(options);
  814. if (Utils.checkParamKeyEmpty(options, 'address')) {
  815. return;
  816. }
  817. var requestParam = {
  818. address: options.address,
  819. output: 'json',
  820. key: that.key
  821. };
  822. //城市限定
  823. if (options.region) {
  824. requestParam.region = options.region;
  825. }
  826. if (options.sig) {
  827. requestParam.sig = Utils.getSig(requestParam, options.sig, 'geocoder');
  828. }
  829. wx.request(Utils.buildWxRequestConfig(options, {
  830. url: URL_GET_GEOCODER,
  831. data: requestParam
  832. },'geocoder'));
  833. };
  834. /**
  835. * 获取城市列表
  836. *
  837. * @param {Object} options 接口参数对象
  838. *
  839. * 请求参数结构可以参考
  840. * http://lbs.qq.com/webservice_v1/guide-region.html
  841. */
  842. getCityList(options) {
  843. var that = this;
  844. options = options || {};
  845. Utils.polyfillParam(options);
  846. var requestParam = {
  847. output: 'json',
  848. key: that.key
  849. };
  850. if (options.sig) {
  851. requestParam.sig = Utils.getSig(requestParam, options.sig, 'getCityList');
  852. }
  853. wx.request(Utils.buildWxRequestConfig(options, {
  854. url: URL_CITY_LIST,
  855. data: requestParam
  856. },'getCityList'));
  857. };
  858. /**
  859. * 获取对应城市ID的区县列表
  860. *
  861. * @param {Object} options 接口参数对象
  862. *
  863. * 请求参数结构可以参考
  864. * http://lbs.qq.com/webservice_v1/guide-region.html
  865. */
  866. getDistrictByCityId(options) {
  867. var that = this;
  868. options = options || {};
  869. Utils.polyfillParam(options);
  870. if (Utils.checkParamKeyEmpty(options, 'id')) {
  871. return;
  872. }
  873. var requestParam = {
  874. id: options.id || '',
  875. output: 'json',
  876. key: that.key
  877. };
  878. if (options.sig) {
  879. requestParam.sig = Utils.getSig(requestParam, options.sig, 'getDistrictByCityId');
  880. }
  881. wx.request(Utils.buildWxRequestConfig(options, {
  882. url: URL_AREA_LIST,
  883. data: requestParam
  884. },'getDistrictByCityId'));
  885. };
  886. /**
  887. * 用于单起点到多终点的路线距离(非直线距离)计算:
  888. * 支持两种距离计算方式:步行和驾车。
  889. * 起点到终点最大限制直线距离10公里。
  890. *
  891. * 新增直线距离计算。
  892. *
  893. * @param {Object} options 接口参数对象
  894. *
  895. * 请求参数结构可以参考
  896. * http://lbs.qq.com/webservice_v1/guide-distance.html
  897. */
  898. calculateDistance(options) {
  899. var that = this;
  900. options = options || {};
  901. Utils.polyfillParam(options);
  902. if (Utils.checkParamKeyEmpty(options, 'to')) {
  903. return;
  904. }
  905. var requestParam = {
  906. mode: options.mode || 'walking',
  907. to: Utils.location2query(options.to),
  908. output: 'json',
  909. key: that.key
  910. };
  911. if (options.from) {
  912. options.location = options.from;
  913. }
  914. //计算直线距离
  915. if(requestParam.mode == 'straight'){
  916. var locationsuccess = function (result) {
  917. var locationTo = Utils.getEndLocation(requestParam.to);//处理终点坐标
  918. var data = {
  919. message:"query ok",
  920. result:{
  921. elements:[]
  922. },
  923. status:0
  924. };
  925. for (var i = 0; i < locationTo.length; i++) {
  926. data.result.elements.push({//将坐标存入
  927. distance: Utils.getDistance(result.latitude, result.longitude, locationTo[i].lat, locationTo[i].lng),
  928. duration:0,
  929. from:{
  930. lat: result.latitude,
  931. lng:result.longitude
  932. },
  933. to:{
  934. lat: locationTo[i].lat,
  935. lng: locationTo[i].lng
  936. }
  937. });
  938. }
  939. var calculateResult = data.result.elements;
  940. var distanceResult = [];
  941. for (var i = 0; i < calculateResult.length; i++) {
  942. distanceResult.push(calculateResult[i].distance);
  943. }
  944. return options.success(data,{
  945. calculateResult: calculateResult,
  946. distanceResult: distanceResult
  947. });
  948. };
  949. Utils.locationProcess(options, locationsuccess);
  950. } else {
  951. var locationsuccess = function (result) {
  952. requestParam.from = result.latitude + ',' + result.longitude;
  953. if (options.sig) {
  954. requestParam.sig = Utils.getSig(requestParam, options.sig, 'calculateDistance');
  955. }
  956. wx.request(Utils.buildWxRequestConfig(options, {
  957. url: URL_DISTANCE,
  958. data: requestParam
  959. },'calculateDistance'));
  960. };
  961. Utils.locationProcess(options, locationsuccess);
  962. }
  963. };
  964. /**
  965. * 路线规划:
  966. *
  967. * @param {Object} options 接口参数对象
  968. *
  969. * 请求参数结构可以参考
  970. * https://lbs.qq.com/webservice_v1/guide-road.html
  971. */
  972. direction(options) {
  973. var that = this;
  974. options = options || {};
  975. Utils.polyfillParam(options);
  976. if (Utils.checkParamKeyEmpty(options, 'to')) {
  977. return;
  978. }
  979. var requestParam = {
  980. output: 'json',
  981. key: that.key
  982. };
  983. //to格式处理
  984. if (typeof options.to == 'string') {
  985. requestParam.to = options.to;
  986. } else {
  987. requestParam.to = options.to.latitude + ',' + options.to.longitude;
  988. }
  989. //初始化局部请求域名
  990. var SET_URL_DIRECTION = null;
  991. //设置默认mode属性
  992. options.mode = options.mode || MODE.driving;
  993. //设置请求域名
  994. SET_URL_DIRECTION = URL_DIRECTION + options.mode;
  995. if (options.from) {
  996. options.location = options.from;
  997. }
  998. if (options.mode == MODE.driving) {
  999. if (options.from_poi) {
  1000. requestParam.from_poi = options.from_poi;
  1001. }
  1002. if (options.heading) {
  1003. requestParam.heading = options.heading;
  1004. }
  1005. if (options.speed) {
  1006. requestParam.speed = options.speed;
  1007. }
  1008. if (options.accuracy) {
  1009. requestParam.accuracy = options.accuracy;
  1010. }
  1011. if (options.road_type) {
  1012. requestParam.road_type = options.road_type;
  1013. }
  1014. if (options.to_poi) {
  1015. requestParam.to_poi = options.to_poi;
  1016. }
  1017. if (options.from_track) {
  1018. requestParam.from_track = options.from_track;
  1019. }
  1020. if (options.waypoints) {
  1021. requestParam.waypoints = options.waypoints;
  1022. }
  1023. if (options.policy) {
  1024. requestParam.policy = options.policy;
  1025. }
  1026. if (options.plate_number) {
  1027. requestParam.plate_number = options.plate_number;
  1028. }
  1029. }
  1030. if (options.mode == MODE.transit) {
  1031. if (options.departure_time) {
  1032. requestParam.departure_time = options.departure_time;
  1033. }
  1034. if (options.policy) {
  1035. requestParam.policy = options.policy;
  1036. }
  1037. }
  1038. var locationsuccess = function (result) {
  1039. requestParam.from = result.latitude + ',' + result.longitude;
  1040. if (options.sig) {
  1041. requestParam.sig = Utils.getSig(requestParam, options.sig, 'direction',options.mode);
  1042. }
  1043. wx.request(Utils.buildWxRequestConfig(options, {
  1044. url: SET_URL_DIRECTION,
  1045. data: requestParam
  1046. }, 'direction'));
  1047. };
  1048. Utils.locationProcess(options, locationsuccess);
  1049. }
  1050. };
  1051. module.exports = QQMapWX;