qqmap-wx-jssdk.js 36 KB

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