qqmap-wx-jssdk.js 37 KB

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