amap-wx.js 11 KB

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