qqmap-wx-jssdk.min.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884
  1. var ERROR_CONF = {
  2. KEY_ERR: 311,
  3. KEY_ERR_MSG: 'key格式错误',
  4. PARAM_ERR: 310,
  5. PARAM_ERR_MSG: '请求参数信息有误',
  6. SYSTEM_ERR: 600,
  7. SYSTEM_ERR_MSG: '系统错误',
  8. WX_ERR_CODE: 1000,
  9. WX_OK_CODE: 200
  10. };
  11. var BASE_URL = 'https://apis.map.qq.com/ws/';
  12. var URL_SEARCH = BASE_URL + 'place/v1/search';
  13. var URL_SUGGESTION = BASE_URL + 'place/v1/suggestion';
  14. var URL_GET_GEOCODER = BASE_URL + 'geocoder/v1/';
  15. var URL_CITY_LIST = BASE_URL + 'district/v1/list';
  16. var URL_AREA_LIST = BASE_URL + 'district/v1/getchildren';
  17. var URL_DISTANCE = BASE_URL + 'distance/v1/';
  18. var URL_DIRECTION = BASE_URL + 'direction/v1/';
  19. var MODE = {
  20. driving: 'driving',
  21. transit: 'transit'
  22. };
  23. var EARTH_RADIUS = 6378136.49;
  24. var Utils = {
  25. safeAdd(x, y) {
  26. var lsw = (x & 0xffff) + (y & 0xffff);
  27. var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
  28. return (msw << 16) | (lsw & 0xffff)
  29. },
  30. bitRotateLeft(num, cnt) {
  31. return (num << cnt) | (num >>> (32 - cnt))
  32. },
  33. md5cmn(q, a, b, x, s, t) {
  34. return this.safeAdd(this.bitRotateLeft(this.safeAdd(this.safeAdd(a, q), this.safeAdd(x, t)), s), b)
  35. },
  36. md5ff(a, b, c, d, x, s, t) {
  37. return this.md5cmn((b & c) | (~b & d), a, b, x, s, t)
  38. },
  39. md5gg(a, b, c, d, x, s, t) {
  40. return this.md5cmn((b & d) | (c & ~d), a, b, x, s, t)
  41. },
  42. md5hh(a, b, c, d, x, s, t) {
  43. return this.md5cmn(b ^ c ^ d, a, b, x, s, t)
  44. },
  45. md5ii(a, b, c, d, x, s, t) {
  46. return this.md5cmn(c ^ (b | ~d), a, b, x, s, t)
  47. },
  48. binlMD5(x, len) {
  49. x[len >> 5] |= 0x80 << (len % 32);
  50. x[((len + 64) >>> 9 << 4) + 14] = len;
  51. var i;
  52. var olda;
  53. var oldb;
  54. var oldc;
  55. var oldd;
  56. var a = 1732584193;
  57. var b = -271733879;
  58. var c = -1732584194;
  59. var d = 271733878;
  60. for (i = 0; i < x.length; i += 16) {
  61. olda = a;
  62. oldb = b;
  63. oldc = c;
  64. oldd = d;
  65. a = this.md5ff(a, b, c, d, x[i], 7, -680876936);
  66. d = this.md5ff(d, a, b, c, x[i + 1], 12, -389564586);
  67. c = this.md5ff(c, d, a, b, x[i + 2], 17, 606105819);
  68. b = this.md5ff(b, c, d, a, x[i + 3], 22, -1044525330);
  69. a = this.md5ff(a, b, c, d, x[i + 4], 7, -176418897);
  70. d = this.md5ff(d, a, b, c, x[i + 5], 12, 1200080426);
  71. c = this.md5ff(c, d, a, b, x[i + 6], 17, -1473231341);
  72. b = this.md5ff(b, c, d, a, x[i + 7], 22, -45705983);
  73. a = this.md5ff(a, b, c, d, x[i + 8], 7, 1770035416);
  74. d = this.md5ff(d, a, b, c, x[i + 9], 12, -1958414417);
  75. c = this.md5ff(c, d, a, b, x[i + 10], 17, -42063);
  76. b = this.md5ff(b, c, d, a, x[i + 11], 22, -1990404162);
  77. a = this.md5ff(a, b, c, d, x[i + 12], 7, 1804603682);
  78. d = this.md5ff(d, a, b, c, x[i + 13], 12, -40341101);
  79. c = this.md5ff(c, d, a, b, x[i + 14], 17, -1502002290);
  80. b = this.md5ff(b, c, d, a, x[i + 15], 22, 1236535329);
  81. a = this.md5gg(a, b, c, d, x[i + 1], 5, -165796510);
  82. d = this.md5gg(d, a, b, c, x[i + 6], 9, -1069501632);
  83. c = this.md5gg(c, d, a, b, x[i + 11], 14, 643717713);
  84. b = this.md5gg(b, c, d, a, x[i], 20, -373897302);
  85. a = this.md5gg(a, b, c, d, x[i + 5], 5, -701558691);
  86. d = this.md5gg(d, a, b, c, x[i + 10], 9, 38016083);
  87. c = this.md5gg(c, d, a, b, x[i + 15], 14, -660478335);
  88. b = this.md5gg(b, c, d, a, x[i + 4], 20, -405537848);
  89. a = this.md5gg(a, b, c, d, x[i + 9], 5, 568446438);
  90. d = this.md5gg(d, a, b, c, x[i + 14], 9, -1019803690);
  91. c = this.md5gg(c, d, a, b, x[i + 3], 14, -187363961);
  92. b = this.md5gg(b, c, d, a, x[i + 8], 20, 1163531501);
  93. a = this.md5gg(a, b, c, d, x[i + 13], 5, -1444681467);
  94. d = this.md5gg(d, a, b, c, x[i + 2], 9, -51403784);
  95. c = this.md5gg(c, d, a, b, x[i + 7], 14, 1735328473);
  96. b = this.md5gg(b, c, d, a, x[i + 12], 20, -1926607734);
  97. a = this.md5hh(a, b, c, d, x[i + 5], 4, -378558);
  98. d = this.md5hh(d, a, b, c, x[i + 8], 11, -2022574463);
  99. c = this.md5hh(c, d, a, b, x[i + 11], 16, 1839030562);
  100. b = this.md5hh(b, c, d, a, x[i + 14], 23, -35309556);
  101. a = this.md5hh(a, b, c, d, x[i + 1], 4, -1530992060);
  102. d = this.md5hh(d, a, b, c, x[i + 4], 11, 1272893353);
  103. c = this.md5hh(c, d, a, b, x[i + 7], 16, -155497632);
  104. b = this.md5hh(b, c, d, a, x[i + 10], 23, -1094730640);
  105. a = this.md5hh(a, b, c, d, x[i + 13], 4, 681279174);
  106. d = this.md5hh(d, a, b, c, x[i], 11, -358537222);
  107. c = this.md5hh(c, d, a, b, x[i + 3], 16, -722521979);
  108. b = this.md5hh(b, c, d, a, x[i + 6], 23, 76029189);
  109. a = this.md5hh(a, b, c, d, x[i + 9], 4, -640364487);
  110. d = this.md5hh(d, a, b, c, x[i + 12], 11, -421815835);
  111. c = this.md5hh(c, d, a, b, x[i + 15], 16, 530742520);
  112. b = this.md5hh(b, c, d, a, x[i + 2], 23, -995338651);
  113. a = this.md5ii(a, b, c, d, x[i], 6, -198630844);
  114. d = this.md5ii(d, a, b, c, x[i + 7], 10, 1126891415);
  115. c = this.md5ii(c, d, a, b, x[i + 14], 15, -1416354905);
  116. b = this.md5ii(b, c, d, a, x[i + 5], 21, -57434055);
  117. a = this.md5ii(a, b, c, d, x[i + 12], 6, 1700485571);
  118. d = this.md5ii(d, a, b, c, x[i + 3], 10, -1894986606);
  119. c = this.md5ii(c, d, a, b, x[i + 10], 15, -1051523);
  120. b = this.md5ii(b, c, d, a, x[i + 1], 21, -2054922799);
  121. a = this.md5ii(a, b, c, d, x[i + 8], 6, 1873313359);
  122. d = this.md5ii(d, a, b, c, x[i + 15], 10, -30611744);
  123. c = this.md5ii(c, d, a, b, x[i + 6], 15, -1560198380);
  124. b = this.md5ii(b, c, d, a, x[i + 13], 21, 1309151649);
  125. a = this.md5ii(a, b, c, d, x[i + 4], 6, -145523070);
  126. d = this.md5ii(d, a, b, c, x[i + 11], 10, -1120210379);
  127. c = this.md5ii(c, d, a, b, x[i + 2], 15, 718787259);
  128. b = this.md5ii(b, c, d, a, x[i + 9], 21, -343485551);
  129. a = this.safeAdd(a, olda);
  130. b = this.safeAdd(b, oldb);
  131. c = this.safeAdd(c, oldc);
  132. d = this.safeAdd(d, oldd)
  133. }
  134. return [a, b, c, d]
  135. },
  136. binl2rstr(input) {
  137. var i;
  138. var output = '';
  139. var length32 = input.length * 32;
  140. for (i = 0; i < length32; i += 8) {
  141. output += String.fromCharCode((input[i >> 5] >>> (i % 32)) & 0xff)
  142. }
  143. return output
  144. },
  145. rstr2binl(input) {
  146. var i;
  147. var output = [];
  148. output[(input.length >> 2) - 1] = undefined;
  149. for (i = 0; i < output.length; i += 1) {
  150. output[i] = 0
  151. }
  152. var length8 = input.length * 8;
  153. for (i = 0; i < length8; i += 8) {
  154. output[i >> 5] |= (input.charCodeAt(i / 8) & 0xff) << (i % 32)
  155. }
  156. return output
  157. },
  158. rstrMD5(s) {
  159. return this.binl2rstr(this.binlMD5(this.rstr2binl(s), s.length * 8))
  160. },
  161. rstrHMACMD5(key, data) {
  162. var i;
  163. var bkey = this.rstr2binl(key);
  164. var ipad = [];
  165. var opad = [];
  166. var hash;
  167. ipad[15] = opad[15] = undefined;
  168. if (bkey.length > 16) {
  169. bkey = this.binlMD5(bkey, key.length * 8)
  170. }
  171. for (i = 0; i < 16; i += 1) {
  172. ipad[i] = bkey[i] ^ 0x36363636;
  173. opad[i] = bkey[i] ^ 0x5c5c5c5c
  174. }
  175. hash = this.binlMD5(ipad.concat(this.rstr2binl(data)), 512 + data.length * 8);
  176. return this.binl2rstr(this.binlMD5(opad.concat(hash), 512 + 128))
  177. },
  178. rstr2hex(input) {
  179. var hexTab = '0123456789abcdef';
  180. var output = '';
  181. var x;
  182. var i;
  183. for (i = 0; i < input.length; i += 1) {
  184. x = input.charCodeAt(i);
  185. output += hexTab.charAt((x >>> 4) & 0x0f) + hexTab.charAt(x & 0x0f)
  186. }
  187. return output
  188. },
  189. str2rstrUTF8(input) {
  190. return unescape(encodeURIComponent(input))
  191. },
  192. rawMD5(s) {
  193. return this.rstrMD5(this.str2rstrUTF8(s))
  194. },
  195. hexMD5(s) {
  196. return this.rstr2hex(this.rawMD5(s))
  197. },
  198. rawHMACMD5(k, d) {
  199. return this.rstrHMACMD5(this.str2rstrUTF8(k), str2rstrUTF8(d))
  200. },
  201. hexHMACMD5(k, d) {
  202. return this.rstr2hex(this.rawHMACMD5(k, d))
  203. },
  204. md5(string, key, raw) {
  205. if (!key) {
  206. if (!raw) {
  207. return this.hexMD5(string)
  208. }
  209. return this.rawMD5(string)
  210. }
  211. if (!raw) {
  212. return this.hexHMACMD5(key, string)
  213. }
  214. return this.rawHMACMD5(key, string)
  215. },
  216. getSig(requestParam, sk, feature, mode) {
  217. var sig = null;
  218. var requestArr = [];
  219. Object.keys(requestParam).sort().forEach(function(key) {
  220. requestArr.push(key + '=' + requestParam[key])
  221. });
  222. if (feature == 'search') {
  223. sig = '/ws/place/v1/search?' + requestArr.join('&') + sk
  224. }
  225. if (feature == 'suggest') {
  226. sig = '/ws/place/v1/suggestion?' + requestArr.join('&') + sk
  227. }
  228. if (feature == 'reverseGeocoder') {
  229. sig = '/ws/geocoder/v1/?' + requestArr.join('&') + sk
  230. }
  231. if (feature == 'geocoder') {
  232. sig = '/ws/geocoder/v1/?' + requestArr.join('&') + sk
  233. }
  234. if (feature == 'getCityList') {
  235. sig = '/ws/district/v1/list?' + requestArr.join('&') + sk
  236. }
  237. if (feature == 'getDistrictByCityId') {
  238. sig = '/ws/district/v1/getchildren?' + requestArr.join('&') + sk
  239. }
  240. if (feature == 'calculateDistance') {
  241. sig = '/ws/distance/v1/?' + requestArr.join('&') + sk
  242. }
  243. if (feature == 'direction') {
  244. sig = '/ws/direction/v1/' + mode + '?' + requestArr.join('&') + sk
  245. }
  246. sig = this.md5(sig);
  247. return sig
  248. },
  249. location2query(data) {
  250. if (typeof data == 'string') {
  251. return data
  252. }
  253. var query = '';
  254. for (var i = 0; i < data.length; i++) {
  255. var d = data[i];
  256. if (!!query) {
  257. query += ';'
  258. }
  259. if (d.location) {
  260. query = query + d.location.lat + ',' + d.location.lng
  261. }
  262. if (d.latitude && d.longitude) {
  263. query = query + d.latitude + ',' + d.longitude
  264. }
  265. }
  266. return query
  267. },
  268. rad(d) {
  269. return d * Math.PI / 180.0
  270. },
  271. getEndLocation(location) {
  272. var to = location.split(';');
  273. var endLocation = [];
  274. for (var i = 0; i < to.length; i++) {
  275. endLocation.push({
  276. lat: parseFloat(to[i].split(',')[0]),
  277. lng: parseFloat(to[i].split(',')[1])
  278. })
  279. }
  280. return endLocation
  281. },
  282. getDistance(latFrom, lngFrom, latTo, lngTo) {
  283. var radLatFrom = this.rad(latFrom);
  284. var radLatTo = this.rad(latTo);
  285. var a = radLatFrom - radLatTo;
  286. var b = this.rad(lngFrom) - this.rad(lngTo);
  287. var distance = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLatFrom) * Math.cos(radLatTo) *
  288. Math.pow(Math.sin(b / 2), 2)));
  289. distance = distance * EARTH_RADIUS;
  290. distance = Math.round(distance * 10000) / 10000;
  291. return parseFloat(distance.toFixed(0))
  292. },
  293. getWXLocation(success, fail, complete) {
  294. wx.getLocation({
  295. type: 'gcj02',
  296. success: success,
  297. fail: fail,
  298. complete: complete
  299. })
  300. },
  301. getLocationParam(location) {
  302. if (typeof location == 'string') {
  303. var locationArr = location.split(',');
  304. if (locationArr.length === 2) {
  305. location = {
  306. latitude: location.split(',')[0],
  307. longitude: location.split(',')[1]
  308. }
  309. } else {
  310. location = {}
  311. }
  312. }
  313. return location
  314. },
  315. polyfillParam(param) {
  316. param.success = param.success || function() {};
  317. param.fail = param.fail || function() {};
  318. param.complete = param.complete || function() {}
  319. },
  320. checkParamKeyEmpty(param, key) {
  321. if (!param[key]) {
  322. var errconf = this.buildErrorConfig(ERROR_CONF.PARAM_ERR, ERROR_CONF.PARAM_ERR_MSG + key + '参数格式有误');
  323. param.fail(errconf);
  324. param.complete(errconf);
  325. return true
  326. }
  327. return false
  328. },
  329. checkKeyword(param) {
  330. return !this.checkParamKeyEmpty(param, 'keyword')
  331. },
  332. checkLocation(param) {
  333. var location = this.getLocationParam(param.location);
  334. if (!location || !location.latitude || !location.longitude) {
  335. var errconf = this.buildErrorConfig(ERROR_CONF.PARAM_ERR, ERROR_CONF.PARAM_ERR_MSG + ' location参数格式有误');
  336. param.fail(errconf);
  337. param.complete(errconf);
  338. return false
  339. }
  340. return true
  341. },
  342. buildErrorConfig(errCode, errMsg) {
  343. return {
  344. status: errCode,
  345. message: errMsg
  346. }
  347. },
  348. handleData(param, data, feature) {
  349. if (feature == 'search') {
  350. var searchResult = data.data;
  351. var searchSimplify = [];
  352. for (var i = 0; i < searchResult.length; i++) {
  353. searchSimplify.push({
  354. id: searchResult[i].id || null,
  355. title: searchResult[i].title || null,
  356. latitude: searchResult[i].location && searchResult[i].location.lat || null,
  357. longitude: searchResult[i].location && searchResult[i].location.lng || null,
  358. address: searchResult[i].address || null,
  359. category: searchResult[i].category || null,
  360. tel: searchResult[i].tel || null,
  361. adcode: searchResult[i].ad_info && searchResult[i].ad_info.adcode || null,
  362. city: searchResult[i].ad_info && searchResult[i].ad_info.city || null,
  363. district: searchResult[i].ad_info && searchResult[i].ad_info.district || null,
  364. province: searchResult[i].ad_info && searchResult[i].ad_info.province || null
  365. })
  366. }
  367. param.success(data, {
  368. searchResult: searchResult,
  369. searchSimplify: searchSimplify
  370. })
  371. } else if (feature == 'suggest') {
  372. var suggestResult = data.data;
  373. var suggestSimplify = [];
  374. for (var i = 0; i < suggestResult.length; i++) {
  375. suggestSimplify.push({
  376. adcode: suggestResult[i].adcode || null,
  377. address: suggestResult[i].address || null,
  378. category: suggestResult[i].category || null,
  379. city: suggestResult[i].city || null,
  380. district: suggestResult[i].district || null,
  381. id: suggestResult[i].id || null,
  382. latitude: suggestResult[i].location && suggestResult[i].location.lat || null,
  383. longitude: suggestResult[i].location && suggestResult[i].location.lng || null,
  384. province: suggestResult[i].province || null,
  385. title: suggestResult[i].title || null,
  386. type: suggestResult[i].type || null
  387. })
  388. }
  389. param.success(data, {
  390. suggestResult: suggestResult,
  391. suggestSimplify: suggestSimplify
  392. })
  393. } else if (feature == 'reverseGeocoder') {
  394. var reverseGeocoderResult = data.result;
  395. var reverseGeocoderSimplify = {
  396. address: reverseGeocoderResult.address || null,
  397. latitude: reverseGeocoderResult.location && reverseGeocoderResult.location.lat || null,
  398. longitude: reverseGeocoderResult.location && reverseGeocoderResult.location.lng || null,
  399. adcode: reverseGeocoderResult.ad_info && reverseGeocoderResult.ad_info.adcode || null,
  400. city: reverseGeocoderResult.address_component && reverseGeocoderResult.address_component.city || null,
  401. district: reverseGeocoderResult.address_component && reverseGeocoderResult.address_component.district || null,
  402. nation: reverseGeocoderResult.address_component && reverseGeocoderResult.address_component.nation || null,
  403. province: reverseGeocoderResult.address_component && reverseGeocoderResult.address_component.province || null,
  404. street: reverseGeocoderResult.address_component && reverseGeocoderResult.address_component.street || null,
  405. street_number: reverseGeocoderResult.address_component && reverseGeocoderResult.address_component.street_number ||
  406. null,
  407. recommend: reverseGeocoderResult.formatted_addresses && reverseGeocoderResult.formatted_addresses.recommend ||
  408. null,
  409. rough: reverseGeocoderResult.formatted_addresses && reverseGeocoderResult.formatted_addresses.rough || null
  410. };
  411. if (reverseGeocoderResult.pois) {
  412. var pois = reverseGeocoderResult.pois;
  413. var poisSimplify = [];
  414. for (var i = 0; i < pois.length; i++) {
  415. poisSimplify.push({
  416. id: pois[i].id || null,
  417. title: pois[i].title || null,
  418. latitude: pois[i].location && pois[i].location.lat || null,
  419. longitude: pois[i].location && pois[i].location.lng || null,
  420. address: pois[i].address || null,
  421. category: pois[i].category || null,
  422. adcode: pois[i].ad_info && pois[i].ad_info.adcode || null,
  423. city: pois[i].ad_info && pois[i].ad_info.city || null,
  424. district: pois[i].ad_info && pois[i].ad_info.district || null,
  425. province: pois[i].ad_info && pois[i].ad_info.province || null
  426. })
  427. }
  428. param.success(data, {
  429. reverseGeocoderResult: reverseGeocoderResult,
  430. reverseGeocoderSimplify: reverseGeocoderSimplify,
  431. pois: pois,
  432. poisSimplify: poisSimplify
  433. })
  434. } else {
  435. param.success(data, {
  436. reverseGeocoderResult: reverseGeocoderResult,
  437. reverseGeocoderSimplify: reverseGeocoderSimplify
  438. })
  439. }
  440. } else if (feature == 'geocoder') {
  441. var geocoderResult = data.result;
  442. var geocoderSimplify = {
  443. title: geocoderResult.title || null,
  444. latitude: geocoderResult.location && geocoderResult.location.lat || null,
  445. longitude: geocoderResult.location && geocoderResult.location.lng || null,
  446. adcode: geocoderResult.ad_info && geocoderResult.ad_info.adcode || null,
  447. province: geocoderResult.address_components && geocoderResult.address_components.province || null,
  448. city: geocoderResult.address_components && geocoderResult.address_components.city || null,
  449. district: geocoderResult.address_components && geocoderResult.address_components.district || null,
  450. street: geocoderResult.address_components && geocoderResult.address_components.street || null,
  451. street_number: geocoderResult.address_components && geocoderResult.address_components.street_number || null,
  452. level: geocoderResult.level || null
  453. };
  454. param.success(data, {
  455. geocoderResult: geocoderResult,
  456. geocoderSimplify: geocoderSimplify
  457. })
  458. } else if (feature == 'getCityList') {
  459. var provinceResult = data.result[0];
  460. var cityResult = data.result[1];
  461. var districtResult = data.result[2];
  462. param.success(data, {
  463. provinceResult: provinceResult,
  464. cityResult: cityResult,
  465. districtResult: districtResult
  466. })
  467. } else if (feature == 'getDistrictByCityId') {
  468. var districtByCity = data.result[0];
  469. param.success(data, districtByCity)
  470. } else if (feature == 'calculateDistance') {
  471. var calculateDistanceResult = data.result.elements;
  472. var distance = [];
  473. for (var i = 0; i < calculateDistanceResult.length; i++) {
  474. distance.push(calculateDistanceResult[i].distance)
  475. }
  476. param.success(data, {
  477. calculateDistanceResult: calculateDistanceResult,
  478. distance: distance
  479. })
  480. } else if (feature == 'direction') {
  481. var direction = data.result.routes;
  482. param.success(data, direction)
  483. } else {
  484. param.success(data)
  485. }
  486. },
  487. buildWxRequestConfig(param, options, feature) {
  488. var that = this;
  489. options.header = {
  490. "content-type": "application/json"
  491. };
  492. options.method = 'GET';
  493. options.success = function(res) {
  494. var data = res.data;
  495. if (data.status === 0) {
  496. that.handleData(param, data, feature)
  497. } else {
  498. param.fail(data)
  499. }
  500. };
  501. options.fail = function(res) {
  502. res.statusCode = ERROR_CONF.WX_ERR_CODE;
  503. param.fail(that.buildErrorConfig(ERROR_CONF.WX_ERR_CODE, res.errMsg))
  504. };
  505. options.complete = function(res) {
  506. var statusCode = +res.statusCode;
  507. switch (statusCode) {
  508. case ERROR_CONF.WX_ERR_CODE:
  509. {
  510. param.complete(that.buildErrorConfig(ERROR_CONF.WX_ERR_CODE, res.errMsg));
  511. break
  512. }
  513. case ERROR_CONF.WX_OK_CODE:
  514. {
  515. var data = res.data;
  516. if (data.status === 0) {
  517. param.complete(data)
  518. } else {
  519. param.complete(that.buildErrorConfig(data.status, data.message))
  520. }
  521. break
  522. }
  523. default:
  524. {
  525. param.complete(that.buildErrorConfig(ERROR_CONF.SYSTEM_ERR, ERROR_CONF.SYSTEM_ERR_MSG))
  526. }
  527. }
  528. };
  529. return options
  530. },
  531. locationProcess(param, locationsuccess, locationfail, locationcomplete) {
  532. var that = this;
  533. locationfail = locationfail || function(res) {
  534. res.statusCode = ERROR_CONF.WX_ERR_CODE;
  535. param.fail(that.buildErrorConfig(ERROR_CONF.WX_ERR_CODE, res.errMsg))
  536. };
  537. locationcomplete = locationcomplete || function(res) {
  538. if (res.statusCode == ERROR_CONF.WX_ERR_CODE) {
  539. param.complete(that.buildErrorConfig(ERROR_CONF.WX_ERR_CODE, res.errMsg))
  540. }
  541. };
  542. if (!param.location) {
  543. that.getWXLocation(locationsuccess, locationfail, locationcomplete)
  544. } else if (that.checkLocation(param)) {
  545. var location = Utils.getLocationParam(param.location);
  546. locationsuccess(location)
  547. }
  548. }
  549. };
  550. class QQMapWX {
  551. constructor(options) {
  552. if (!options.key) {
  553. throw Error('key值不能为空')
  554. }
  555. this.key = options.key
  556. };
  557. search(options) {
  558. var that = this;
  559. options = options || {};
  560. Utils.polyfillParam(options);
  561. if (!Utils.checkKeyword(options)) {
  562. return
  563. }
  564. var requestParam = {
  565. keyword: options.keyword,
  566. orderby: options.orderby || '_distance',
  567. page_size: options.page_size || 10,
  568. page_index: options.page_index || 1,
  569. output: 'json',
  570. key: that.key
  571. };
  572. if (options.address_format) {
  573. requestParam.address_format = options.address_format
  574. }
  575. if (options.filter) {
  576. requestParam.filter = options.filter
  577. }
  578. var distance = options.distance || "1000";
  579. var auto_extend = options.auto_extend || 1;
  580. var region = null;
  581. var rectangle = null;
  582. if (options.region) {
  583. region = options.region
  584. }
  585. if (options.rectangle) {
  586. rectangle = options.rectangle
  587. }
  588. var locationsuccess = function(result) {
  589. if (region && !rectangle) {
  590. requestParam.boundary = "region(" + region + "," + auto_extend + "," + result.latitude + "," + result.longitude +
  591. ")";
  592. if (options.sig) {
  593. requestParam.sig = Utils.getSig(requestParam, options.sig, 'search')
  594. }
  595. } else if (rectangle && !region) {
  596. requestParam.boundary = "rectangle(" + rectangle + ")";
  597. if (options.sig) {
  598. requestParam.sig = Utils.getSig(requestParam, options.sig, 'search')
  599. }
  600. } else {
  601. requestParam.boundary = "nearby(" + result.latitude + "," + result.longitude + "," + distance + "," + auto_extend +
  602. ")";
  603. if (options.sig) {
  604. requestParam.sig = Utils.getSig(requestParam, options.sig, 'search')
  605. }
  606. }
  607. wx.request(Utils.buildWxRequestConfig(options, {
  608. url: URL_SEARCH,
  609. data: requestParam
  610. }, 'search'))
  611. };
  612. Utils.locationProcess(options, locationsuccess)
  613. };
  614. getSuggestion(options) {
  615. var that = this;
  616. options = options || {};
  617. Utils.polyfillParam(options);
  618. if (!Utils.checkKeyword(options)) {
  619. return
  620. }
  621. var requestParam = {
  622. keyword: options.keyword,
  623. region: options.region || '全国',
  624. region_fix: options.region_fix || 0,
  625. policy: options.policy || 0,
  626. page_size: options.page_size || 10,
  627. page_index: options.page_index || 1,
  628. get_subpois: options.get_subpois || 0,
  629. output: 'json',
  630. key: that.key
  631. };
  632. if (options.address_format) {
  633. requestParam.address_format = options.address_format
  634. }
  635. if (options.filter) {
  636. requestParam.filter = options.filter
  637. }
  638. if (options.location) {
  639. var locationsuccess = function(result) {
  640. requestParam.location = result.latitude + ',' + result.longitude;
  641. if (options.sig) {
  642. requestParam.sig = Utils.getSig(requestParam, options.sig, 'suggest')
  643. }
  644. wx.request(Utils.buildWxRequestConfig(options, {
  645. url: URL_SUGGESTION,
  646. data: requestParam
  647. }, "suggest"))
  648. };
  649. Utils.locationProcess(options, locationsuccess)
  650. } else {
  651. if (options.sig) {
  652. requestParam.sig = Utils.getSig(requestParam, options.sig, 'suggest')
  653. }
  654. wx.request(Utils.buildWxRequestConfig(options, {
  655. url: URL_SUGGESTION,
  656. data: requestParam
  657. }, "suggest"))
  658. }
  659. };
  660. reverseGeocoder(options) {
  661. var that = this;
  662. options = options || {};
  663. Utils.polyfillParam(options);
  664. var requestParam = {
  665. coord_type: options.coord_type || 5,
  666. get_poi: options.get_poi || 0,
  667. output: 'json',
  668. key: that.key
  669. };
  670. if (options.poi_options) {
  671. requestParam.poi_options = options.poi_options
  672. }
  673. var locationsuccess = function(result) {
  674. requestParam.location = result.latitude + ',' + result.longitude;
  675. if (options.sig) {
  676. requestParam.sig = Utils.getSig(requestParam, options.sig, 'reverseGeocoder')
  677. }
  678. wx.request(Utils.buildWxRequestConfig(options, {
  679. url: URL_GET_GEOCODER,
  680. data: requestParam
  681. }, 'reverseGeocoder'))
  682. };
  683. Utils.locationProcess(options, locationsuccess)
  684. };
  685. geocoder(options) {
  686. var that = this;
  687. options = options || {};
  688. Utils.polyfillParam(options);
  689. if (Utils.checkParamKeyEmpty(options, 'address')) {
  690. return
  691. }
  692. var requestParam = {
  693. address: options.address,
  694. output: 'json',
  695. key: that.key
  696. };
  697. if (options.region) {
  698. requestParam.region = options.region
  699. }
  700. if (options.sig) {
  701. requestParam.sig = Utils.getSig(requestParam, options.sig, 'geocoder')
  702. }
  703. wx.request(Utils.buildWxRequestConfig(options, {
  704. url: URL_GET_GEOCODER,
  705. data: requestParam
  706. }, 'geocoder'))
  707. };
  708. getCityList(options) {
  709. var that = this;
  710. options = options || {};
  711. Utils.polyfillParam(options);
  712. var requestParam = {
  713. output: 'json',
  714. key: that.key
  715. };
  716. if (options.sig) {
  717. requestParam.sig = Utils.getSig(requestParam, options.sig, 'getCityList')
  718. }
  719. wx.request(Utils.buildWxRequestConfig(options, {
  720. url: URL_CITY_LIST,
  721. data: requestParam
  722. }, 'getCityList'))
  723. };
  724. getDistrictByCityId(options) {
  725. var that = this;
  726. options = options || {};
  727. Utils.polyfillParam(options);
  728. if (Utils.checkParamKeyEmpty(options, 'id')) {
  729. return
  730. }
  731. var requestParam = {
  732. id: options.id || '',
  733. output: 'json',
  734. key: that.key
  735. };
  736. if (options.sig) {
  737. requestParam.sig = Utils.getSig(requestParam, options.sig, 'getDistrictByCityId')
  738. }
  739. wx.request(Utils.buildWxRequestConfig(options, {
  740. url: URL_AREA_LIST,
  741. data: requestParam
  742. }, 'getDistrictByCityId'))
  743. };
  744. calculateDistance(options) {
  745. var that = this;
  746. options = options || {};
  747. Utils.polyfillParam(options);
  748. if (Utils.checkParamKeyEmpty(options, 'to')) {
  749. return
  750. }
  751. var requestParam = {
  752. mode: options.mode || 'walking',
  753. to: Utils.location2query(options.to),
  754. output: 'json',
  755. key: that.key
  756. };
  757. if (options.from) {
  758. options.location = options.from
  759. }
  760. if (requestParam.mode == 'straight') {
  761. var locationsuccess = function(result) {
  762. var locationTo = Utils.getEndLocation(requestParam.to);
  763. var data = {
  764. message: "query ok",
  765. result: {
  766. elements: []
  767. },
  768. status: 0
  769. };
  770. for (var i = 0; i < locationTo.length; i++) {
  771. data.result.elements.push({
  772. distance: Utils.getDistance(result.latitude, result.longitude, locationTo[i].lat, locationTo[i].lng),
  773. duration: 0,
  774. from: {
  775. lat: result.latitude,
  776. lng: result.longitude
  777. },
  778. to: {
  779. lat: locationTo[i].lat,
  780. lng: locationTo[i].lng
  781. }
  782. })
  783. }
  784. var calculateResult = data.result.elements;
  785. var distanceResult = [];
  786. for (var i = 0; i < calculateResult.length; i++) {
  787. distanceResult.push(calculateResult[i].distance)
  788. }
  789. return options.success(data, {
  790. calculateResult: calculateResult,
  791. distanceResult: distanceResult
  792. })
  793. };
  794. Utils.locationProcess(options, locationsuccess)
  795. } else {
  796. var locationsuccess = function(result) {
  797. requestParam.from = result.latitude + ',' + result.longitude;
  798. if (options.sig) {
  799. requestParam.sig = Utils.getSig(requestParam, options.sig, 'calculateDistance')
  800. }
  801. wx.request(Utils.buildWxRequestConfig(options, {
  802. url: URL_DISTANCE,
  803. data: requestParam
  804. }, 'calculateDistance'))
  805. };
  806. Utils.locationProcess(options, locationsuccess)
  807. }
  808. };
  809. direction(options) {
  810. var that = this;
  811. options = options || {};
  812. Utils.polyfillParam(options);
  813. if (Utils.checkParamKeyEmpty(options, 'to')) {
  814. return
  815. }
  816. var requestParam = {
  817. output: 'json',
  818. key: that.key
  819. };
  820. if (typeof options.to == 'string') {
  821. requestParam.to = options.to
  822. } else {
  823. requestParam.to = options.to.latitude + ',' + options.to.longitude
  824. }
  825. var SET_URL_DIRECTION = null;
  826. options.mode = options.mode || MODE.driving;
  827. SET_URL_DIRECTION = URL_DIRECTION + options.mode;
  828. if (options.from) {
  829. options.location = options.from
  830. }
  831. if (options.mode == MODE.driving) {
  832. if (options.from_poi) {
  833. requestParam.from_poi = options.from_poi
  834. }
  835. if (options.heading) {
  836. requestParam.heading = options.heading
  837. }
  838. if (options.speed) {
  839. requestParam.speed = options.speed
  840. }
  841. if (options.accuracy) {
  842. requestParam.accuracy = options.accuracy
  843. }
  844. if (options.road_type) {
  845. requestParam.road_type = options.road_type
  846. }
  847. if (options.to_poi) {
  848. requestParam.to_poi = options.to_poi
  849. }
  850. if (options.from_track) {
  851. requestParam.from_track = options.from_track
  852. }
  853. if (options.waypoints) {
  854. requestParam.waypoints = options.waypoints
  855. }
  856. if (options.policy) {
  857. requestParam.policy = options.policy
  858. }
  859. if (options.plate_number) {
  860. requestParam.plate_number = options.plate_number
  861. }
  862. }
  863. if (options.mode == MODE.transit) {
  864. if (options.departure_time) {
  865. requestParam.departure_time = options.departure_time
  866. }
  867. if (options.policy) {
  868. requestParam.policy = options.policy
  869. }
  870. }
  871. var locationsuccess = function(result) {
  872. requestParam.from = result.latitude + ',' + result.longitude;
  873. if (options.sig) {
  874. requestParam.sig = Utils.getSig(requestParam, options.sig, 'direction', options.mode)
  875. }
  876. wx.request(Utils.buildWxRequestConfig(options, {
  877. url: SET_URL_DIRECTION,
  878. data: requestParam
  879. }, 'direction'))
  880. };
  881. Utils.locationProcess(options, locationsuccess)
  882. }
  883. };
  884. module.exports = QQMapWX;