amap-wx.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. function AMapWX(a) {
  2. this.key = a.key, this.requestConfig = {
  3. key: a.key,
  4. s: "rsx",
  5. platform: "WXJS",
  6. appname: a.key,
  7. sdkversion: "1.2.0",
  8. logversion: "2.0"
  9. }
  10. }
  11. AMapWX.prototype.getWxLocation = function(a, b) {
  12. wx.getLocation({
  13. type: "gcj02",
  14. success: function(a) {
  15. var c = a.longitude + "," + a.latitude;
  16. wx.setStorage({
  17. key: "userLocation",
  18. data: c
  19. }), b(c)
  20. },
  21. fail: function(c) {
  22. wx.getStorage({
  23. key: "userLocation",
  24. success: function(a) {
  25. a.data && b(a.data)
  26. }
  27. }), a.fail({
  28. errCode: "0",
  29. errMsg: c.errMsg || ""
  30. })
  31. }
  32. })
  33. }, AMapWX.prototype.getRegeo = function(a) {
  34. function c(c) {
  35. var d = b.requestConfig;
  36. wx.request({
  37. url: "https://restapi.amap.com/v3/geocode/regeo",
  38. data: {
  39. key: b.key,
  40. location: c,
  41. extensions: "all",
  42. s: d.s,
  43. platform: d.platform,
  44. appname: b.key,
  45. sdkversion: d.sdkversion,
  46. logversion: d.logversion
  47. },
  48. method: "GET",
  49. header: {
  50. "content-type": "application/json"
  51. },
  52. success: function(b) {
  53. var d, e, f, g, h, i, j, k, l;
  54. b.data.status && "1" == b.data.status ? (d = b.data.regeocode, e = d.addressComponent, f = [], g = "", d && d.roads[
  55. 0] && d.roads[0].name && (g = d.roads[0].name + "附近"), h = c.split(",")[0], i = c.split(",")[1], d.pois && d
  56. .pois[0] && (g = d.pois[0].name + "附近", j = d.pois[0].location, j && (h = parseFloat(j.split(",")[0]), i =
  57. parseFloat(j.split(",")[1]))), e.provice && f.push(e.provice), e.city && f.push(e.city), e.district && f.push(
  58. e.district), e.streetNumber && e.streetNumber.street && e.streetNumber.number ? (f.push(e.streetNumber.street),
  59. f.push(e.streetNumber.number)) : (k = "", d && d.roads[0] && d.roads[0].name && (k = d.roads[0].name), f.push(
  60. k)), f = f.join(""), l = [{
  61. iconPath: a.iconPath,
  62. width: a.iconWidth,
  63. height: a.iconHeight,
  64. name: f,
  65. desc: g,
  66. longitude: h,
  67. latitude: i,
  68. id: 0,
  69. regeocodeData: d
  70. }], a.success(l)) : a.fail({
  71. errCode: b.data.infocode,
  72. errMsg: b.data.info
  73. })
  74. },
  75. fail: function(b) {
  76. a.fail({
  77. errCode: "0",
  78. errMsg: b.errMsg || ""
  79. })
  80. }
  81. })
  82. }
  83. var b = this;
  84. a.location ? c(a.location) : b.getWxLocation(a, function(a) {
  85. c(a)
  86. })
  87. }, AMapWX.prototype.getWeather = function(a) {
  88. function d(d) {
  89. var e = "base";
  90. a.type && "forecast" == a.type && (e = "all"), wx.request({
  91. url: "https://restapi.amap.com/v3/weather/weatherInfo",
  92. data: {
  93. key: b.key,
  94. city: d,
  95. extensions: e,
  96. s: c.s,
  97. platform: c.platform,
  98. appname: b.key,
  99. sdkversion: c.sdkversion,
  100. logversion: c.logversion
  101. },
  102. method: "GET",
  103. header: {
  104. "content-type": "application/json"
  105. },
  106. success: function(b) {
  107. function c(a) {
  108. var b = {
  109. city: {
  110. text: "城市",
  111. data: a.city
  112. },
  113. weather: {
  114. text: "天气",
  115. data: a.weather
  116. },
  117. temperature: {
  118. text: "温度",
  119. data: a.temperature
  120. },
  121. winddirection: {
  122. text: "风向",
  123. data: a.winddirection + "风"
  124. },
  125. windpower: {
  126. text: "风力",
  127. data: a.windpower + "级"
  128. },
  129. humidity: {
  130. text: "湿度",
  131. data: a.humidity + "%"
  132. }
  133. };
  134. return b
  135. }
  136. var d, e;
  137. b.data.status && "1" == b.data.status ? b.data.lives ? (d = b.data.lives, d && d.length > 0 && (d = d[0], e = c(
  138. d), e["liveData"] = d, a.success(e))) : b.data.forecasts && b.data.forecasts[0] && a.success({
  139. forecast: b.data.forecasts[0]
  140. }) : a.fail({
  141. errCode: b.data.infocode,
  142. errMsg: b.data.info
  143. })
  144. },
  145. fail: function(b) {
  146. a.fail({
  147. errCode: "0",
  148. errMsg: b.errMsg || ""
  149. })
  150. }
  151. })
  152. }
  153. function e(e) {
  154. wx.request({
  155. url: "https://restapi.amap.com/v3/geocode/regeo",
  156. data: {
  157. key: b.key,
  158. location: e,
  159. extensions: "all",
  160. s: c.s,
  161. platform: c.platform,
  162. appname: b.key,
  163. sdkversion: c.sdkversion,
  164. logversion: c.logversion
  165. },
  166. method: "GET",
  167. header: {
  168. "content-type": "application/json"
  169. },
  170. success: function(b) {
  171. var c, e;
  172. b.data.status && "1" == b.data.status ? (e = b.data.regeocode, e.addressComponent ? c = e.addressComponent.adcode :
  173. e.aois && e.aois.length > 0 && (c = e.aois[0].adcode), d(c)) : a.fail({
  174. errCode: b.data.infocode,
  175. errMsg: b.data.info
  176. })
  177. },
  178. fail: function(b) {
  179. a.fail({
  180. errCode: "0",
  181. errMsg: b.errMsg || ""
  182. })
  183. }
  184. })
  185. }
  186. var b = this,
  187. c = b.requestConfig;
  188. a.city ? d(a.city) : b.getWxLocation(a, function(a) {
  189. e(a)
  190. })
  191. }, AMapWX.prototype.getPoiAround = function(a) {
  192. function d(d) {
  193. var e = {
  194. key: b.key,
  195. location: d,
  196. s: c.s,
  197. platform: c.platform,
  198. appname: b.key,
  199. sdkversion: c.sdkversion,
  200. logversion: c.logversion
  201. };
  202. a.querytypes && (e["types"] = a.querytypes), a.querykeywords && (e["keywords"] = a.querykeywords), wx.request({
  203. url: "https://restapi.amap.com/v3/place/around",
  204. data: e,
  205. method: "GET",
  206. header: {
  207. "content-type": "application/json"
  208. },
  209. success: function(b) {
  210. var c, d, e, f;
  211. if (b.data.status && "1" == b.data.status) {
  212. if (b = b.data, b && b.pois) {
  213. for (c = [], d = 0; d < b.pois.length; d++) e = 0 == d ? a.iconPathSelected : a.iconPath, c.push({
  214. latitude: parseFloat(b.pois[d].location.split(",")[1]),
  215. longitude: parseFloat(b.pois[d].location.split(",")[0]),
  216. iconPath: e,
  217. width: 22,
  218. height: 32,
  219. id: d,
  220. name: b.pois[d].name,
  221. address: b.pois[d].address
  222. });
  223. f = {
  224. markers: c,
  225. poisData: b.pois
  226. }, a.success(f)
  227. }
  228. } else a.fail({
  229. errCode: b.data.infocode,
  230. errMsg: b.data.info
  231. })
  232. },
  233. fail: function(b) {
  234. a.fail({
  235. errCode: "0",
  236. errMsg: b.errMsg || ""
  237. })
  238. }
  239. })
  240. }
  241. var b = this,
  242. c = b.requestConfig;
  243. a.location ? d(a.location) : b.getWxLocation(a, function(a) {
  244. d(a)
  245. })
  246. }, AMapWX.prototype.getStaticmap = function(a) {
  247. function f(b) {
  248. c.push("location=" + b), a.zoom && c.push("zoom=" + a.zoom), a.size && c.push("size=" + a.size), a.scale && c.push(
  249. "scale=" + a.scale), a.markers && c.push("markers=" + a.markers), a.labels && c.push("labels=" + a.labels), a.paths &&
  250. c.push("paths=" + a.paths), a.traffic && c.push("traffic=" + a.traffic);
  251. var e = d + c.join("&");
  252. a.success({
  253. url: e
  254. })
  255. }
  256. var e, b = this,
  257. c = [],
  258. d = "https://restapi.amap.com/v3/staticmap?";
  259. c.push("key=" + b.key), e = b.requestConfig, c.push("s=" + e.s), c.push("platform=" + e.platform), c.push("appname=" +
  260. e.appname), c.push("sdkversion=" + e.sdkversion), c.push("logversion=" + e.logversion), a.location ? f(a.location) :
  261. b.getWxLocation(a, function(a) {
  262. f(a)
  263. })
  264. }, AMapWX.prototype.getInputtips = function(a) {
  265. var b = this,
  266. c = b.requestConfig,
  267. d = {
  268. key: b.key,
  269. s: c.s,
  270. platform: c.platform,
  271. appname: b.key,
  272. sdkversion: c.sdkversion,
  273. logversion: c.logversion
  274. };
  275. a.location && (d["location"] = a.location), a.keywords && (d["keywords"] = a.keywords), a.type && (d["type"] = a.type),
  276. a.city && (d["city"] = a.city), a.citylimit && (d["citylimit"] = a.citylimit), wx.request({
  277. url: "https://restapi.amap.com/v3/assistant/inputtips",
  278. data: d,
  279. method: "GET",
  280. header: {
  281. "content-type": "application/json"
  282. },
  283. success: function(b) {
  284. b && b.data && b.data.tips && a.success({
  285. tips: b.data.tips
  286. })
  287. },
  288. fail: function(b) {
  289. a.fail({
  290. errCode: "0",
  291. errMsg: b.errMsg || ""
  292. })
  293. }
  294. })
  295. }, AMapWX.prototype.getDrivingRoute = function(a) {
  296. var b = this,
  297. c = b.requestConfig,
  298. d = {
  299. key: b.key,
  300. s: c.s,
  301. platform: c.platform,
  302. appname: b.key,
  303. sdkversion: c.sdkversion,
  304. logversion: c.logversion
  305. };
  306. a.origin && (d["origin"] = a.origin), a.destination && (d["destination"] = a.destination), a.strategy && (d[
  307. "strategy"] = a.strategy), a.waypoints && (d["waypoints"] = a.waypoints), a.avoidpolygons && (d["avoidpolygons"] =
  308. a.avoidpolygons), a.avoidroad && (d["avoidroad"] = a.avoidroad), wx.request({
  309. url: "https://restapi.amap.com/v3/direction/driving",
  310. data: d,
  311. method: "GET",
  312. header: {
  313. "content-type": "application/json"
  314. },
  315. success: function(b) {
  316. b && b.data && b.data.route && a.success({
  317. paths: b.data.route.paths,
  318. taxi_cost: b.data.route.taxi_cost || ""
  319. })
  320. },
  321. fail: function(b) {
  322. a.fail({
  323. errCode: "0",
  324. errMsg: b.errMsg || ""
  325. })
  326. }
  327. })
  328. }, AMapWX.prototype.getWalkingRoute = function(a) {
  329. var b = this,
  330. c = b.requestConfig,
  331. d = {
  332. key: b.key,
  333. s: c.s,
  334. platform: c.platform,
  335. appname: b.key,
  336. sdkversion: c.sdkversion,
  337. logversion: c.logversion
  338. };
  339. a.origin && (d["origin"] = a.origin), a.destination && (d["destination"] = a.destination), wx.request({
  340. url: "https://restapi.amap.com/v3/direction/walking",
  341. data: d,
  342. method: "GET",
  343. header: {
  344. "content-type": "application/json"
  345. },
  346. success: function(b) {
  347. b && b.data && b.data.route && a.success({
  348. paths: b.data.route.paths
  349. })
  350. },
  351. fail: function(b) {
  352. a.fail({
  353. errCode: "0",
  354. errMsg: b.errMsg || ""
  355. })
  356. }
  357. })
  358. }, AMapWX.prototype.getTransitRoute = function(a) {
  359. var b = this,
  360. c = b.requestConfig,
  361. d = {
  362. key: b.key,
  363. s: c.s,
  364. platform: c.platform,
  365. appname: b.key,
  366. sdkversion: c.sdkversion,
  367. logversion: c.logversion
  368. };
  369. a.origin && (d["origin"] = a.origin), a.destination && (d["destination"] = a.destination), a.strategy && (d[
  370. "strategy"] = a.strategy), a.city && (d["city"] = a.city), a.cityd && (d["cityd"] = a.cityd), wx.request({
  371. url: "https://restapi.amap.com/v3/direction/transit/integrated",
  372. data: d,
  373. method: "GET",
  374. header: {
  375. "content-type": "application/json"
  376. },
  377. success: function(b) {
  378. if (b && b.data && b.data.route) {
  379. var c = b.data.route;
  380. a.success({
  381. distance: c.distance || "",
  382. taxi_cost: c.taxi_cost || "",
  383. transits: c.transits
  384. })
  385. }
  386. },
  387. fail: function(b) {
  388. a.fail({
  389. errCode: "0",
  390. errMsg: b.errMsg || ""
  391. })
  392. }
  393. })
  394. }, AMapWX.prototype.getRidingRoute = function(a) {
  395. var b = this,
  396. c = b.requestConfig,
  397. d = {
  398. key: b.key,
  399. s: c.s,
  400. platform: c.platform,
  401. appname: b.key,
  402. sdkversion: c.sdkversion,
  403. logversion: c.logversion
  404. };
  405. a.origin && (d["origin"] = a.origin), a.destination && (d["destination"] = a.destination), wx.request({
  406. url: "https://restapi.amap.com/v4/direction/bicycling",
  407. data: d,
  408. method: "GET",
  409. header: {
  410. "content-type": "application/json"
  411. },
  412. success: function(b) {
  413. b && b.data && b.data.data && a.success({
  414. paths: b.data.data.paths
  415. })
  416. },
  417. fail: function(b) {
  418. a.fail({
  419. errCode: "0",
  420. errMsg: b.errMsg || ""
  421. })
  422. }
  423. })
  424. }, module.exports.AMapWX = AMapWX;