qqmap-wx-jssdk.js 35 KB

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