qqmap-wx-jssdk.min.js 37 KB

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