qqmap.js 37 KB

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