qqmap-wx-jssdk.js 38 KB

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