bmap-wx.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. /**
  2. * @file 微信小程序JSAPI
  3. * @author 崔健 cuijian03@baidu.com 2017.01.10
  4. * @update 邓淑芳 623996689@qq.com 2019.07.03
  5. */
  6. /**
  7. * 百度地图微信小程序API类
  8. *
  9. * @class
  10. */
  11. class BMapWX {
  12. /**
  13. * 百度地图微信小程序API类
  14. *
  15. * @constructor
  16. */
  17. constructor(param) {
  18. this.ak = param["ak"];
  19. }
  20. /**
  21. * 使用微信接口进行定位
  22. *
  23. * @param {string} type 坐标类型
  24. * @param {Function} success 成功执行
  25. * @param {Function} fail 失败执行
  26. * @param {Function} complete 完成后执行
  27. */
  28. getWXLocation(type, success, fail, complete) {
  29. type = type || 'gcj02',
  30. success = success || function() {};
  31. fail = fail || function() {};
  32. complete = complete || function() {};
  33. // wx.getLocation({
  34. // type: type,
  35. // success: success,
  36. // fail: fail,
  37. // complete: complete
  38. // });
  39. wx.getFuzzyLocation({
  40. type: type,
  41. success: success,
  42. fail: fail,
  43. complete: complete
  44. })
  45. }
  46. /**
  47. * POI周边检索
  48. *
  49. * @param {Object} param 检索配置
  50. * 参数对象结构可以参考
  51. * http://lbsyun.baidu.com/index.php?title=webapi/guide/webservice-placeapi
  52. */
  53. search(param) {
  54. var that = this;
  55. param = param || {};
  56. let searchparam = {
  57. query: param["query"] || '生活服务$美食&酒店',
  58. scope: param["scope"] || 1,
  59. filter: param["filter"] || '',
  60. coord_type: param["coord_type"] || 2,
  61. page_size: param["page_size"] || 10,
  62. page_num: param["page_num"] || 0,
  63. output: param["output"] || 'json',
  64. ak: that.ak,
  65. sn: param["sn"] || '',
  66. timestamp: param["timestamp"] || '',
  67. radius: param["radius"] || 2000,
  68. ret_coordtype: 'gcj02ll'
  69. };
  70. let otherparam = {
  71. iconPath: param["iconPath"],
  72. iconTapPath: param["iconTapPath"],
  73. width: param["width"],
  74. height: param["height"],
  75. alpha: param["alpha"] || 1,
  76. success: param["success"] || function() {},
  77. fail: param["fail"] || function() {}
  78. };
  79. let type = 'gcj02';
  80. let locationsuccess = function(result) {
  81. searchparam["location"] = result["latitude"] + ',' + result["longitude"];
  82. wx.request({
  83. url: 'https://api.map.baidu.com/place/v2/search',
  84. data: searchparam,
  85. header: {
  86. "content-type": "application/json"
  87. },
  88. method: 'GET',
  89. success(data) {
  90. let res = data["data"];
  91. if (res["status"] === 0) {
  92. let poiArr = res["results"];
  93. // outputRes 包含两个对象,
  94. // originalData为百度接口返回的原始数据
  95. // wxMarkerData为小程序规范的marker格式
  96. let outputRes = {};
  97. outputRes["originalData"] = res;
  98. outputRes["wxMarkerData"] = [];
  99. for (let i = 0; i < poiArr.length; i++) {
  100. outputRes["wxMarkerData"][i] = {
  101. id: i,
  102. latitude: poiArr[i]["location"]["lat"],
  103. longitude: poiArr[i]["location"]["lng"],
  104. title: poiArr[i]["name"],
  105. iconPath: otherparam["iconPath"],
  106. iconTapPath: otherparam["iconTapPath"],
  107. address: poiArr[i]["address"],
  108. telephone: poiArr[i]["telephone"],
  109. alpha: otherparam["alpha"],
  110. width: otherparam["width"],
  111. height: otherparam["height"]
  112. }
  113. }
  114. otherparam.success(outputRes);
  115. } else {
  116. otherparam.fail({
  117. errMsg: res["message"],
  118. statusCode: res["status"]
  119. });
  120. }
  121. },
  122. fail(data) {
  123. otherparam.fail(data);
  124. }
  125. });
  126. }
  127. let locationfail = function(result) {
  128. otherparam.fail(result);
  129. };
  130. let locationcomplete = function(result) {};
  131. if (!param["location"]) {
  132. that.getWXLocation(type, locationsuccess, locationfail, locationcomplete);
  133. } else {
  134. let longitude = param.location.split(',')[1];
  135. let latitude = param.location.split(',')[0];
  136. let errMsg = 'input location';
  137. let res = {
  138. errMsg: errMsg,
  139. latitude: latitude,
  140. longitude: longitude
  141. };
  142. locationsuccess(res);
  143. }
  144. }
  145. /**
  146. * sug模糊检索
  147. *
  148. * @param {Object} param 检索配置
  149. * 参数对象结构可以参考
  150. * http://lbsyun.baidu.com/index.php?title=webapi/place-suggestion-api
  151. */
  152. suggestion(param) {
  153. var that = this;
  154. param = param || {};
  155. let suggestionparam = {
  156. query: param["query"] || '',
  157. region: param["region"] || '全国',
  158. city_limit: param["city_limit"] || false,
  159. output: param["output"] || 'json',
  160. ak: that.ak,
  161. sn: param["sn"] || '',
  162. timestamp: param["timestamp"] || '',
  163. ret_coordtype: 'gcj02ll'
  164. };
  165. let otherparam = {
  166. success: param["success"] || function() {},
  167. fail: param["fail"] || function() {}
  168. };
  169. wx.request({
  170. url: 'https://api.map.baidu.com/place/v2/suggestion',
  171. data: suggestionparam,
  172. header: {
  173. "content-type": "application/json"
  174. },
  175. method: 'GET',
  176. success(data) {
  177. let res = data["data"];
  178. if (res["status"] === 0) {
  179. otherparam.success(res);
  180. } else {
  181. otherparam.fail({
  182. errMsg: res["message"],
  183. statusCode: res["status"]
  184. });
  185. }
  186. },
  187. fail(data) {
  188. otherparam.fail(data);
  189. }
  190. });
  191. }
  192. /**
  193. * rgc检索(逆地理编码:经纬度->地点描述)
  194. *
  195. * @param {Object} param 检索配置
  196. * 参数对象结构可以参考
  197. * https://lbs.baidu.com/index.php?title=webapi/guide/webservice-geocoding-abroad
  198. *
  199. */
  200. regeocoding(param) {
  201. var that = this;
  202. param = param || {};
  203. let regeocodingparam = {
  204. coordtype: param["coordtype"] || 'gcj02ll',
  205. ret_coordtype: 'gcj02ll',
  206. radius: param["radius"] || 1000,
  207. ak: that.ak,
  208. sn: param["sn"] || '',
  209. output: param["output"] || 'json',
  210. callback: param["callback"] || function() {},
  211. extensions_poi: param["extensions_poi"] || 1,
  212. extensions_road: param["extensions_road"] || false,
  213. extensions_town: param["extensions_town"] || false,
  214. language: param["language"] || 'zh-CN',
  215. language_auto: param["language_auto"] || 0
  216. };
  217. let otherparam = {
  218. iconPath: param["iconPath"],
  219. iconTapPath: param["iconTapPath"],
  220. width: param["width"],
  221. height: param["height"],
  222. alpha: param["alpha"] || 1,
  223. success: param["success"] || function() {},
  224. fail: param["fail"] || function() {}
  225. };
  226. let type = 'gcj02';
  227. let locationsuccess = function(result) {
  228. regeocodingparam["location"] = result["latitude"] + ',' + result["longitude"];
  229. wx.request({
  230. url: 'https://api.map.baidu.com/reverse_geocoding/v3',
  231. data: regeocodingparam,
  232. header: {
  233. "content-type": "application/json"
  234. },
  235. method: 'GET',
  236. success(data) {
  237. let res = data["data"];
  238. if (res["status"] === 0) {
  239. let poiObj = res["result"];
  240. // outputRes 包含两个对象:
  241. // originalData为百度接口返回的原始数据
  242. // wxMarkerData为小程序规范的marker格式
  243. let outputRes = {};
  244. outputRes["originalData"] = res;
  245. outputRes["wxMarkerData"] = [];
  246. outputRes["wxMarkerData"][0] = {
  247. id: 0,
  248. latitude: result["latitude"],
  249. longitude: result["longitude"],
  250. address: poiObj["formatted_address"],
  251. iconPath: otherparam["iconPath"],
  252. iconTapPath: otherparam["iconTapPath"],
  253. desc: poiObj["sematic_description"],
  254. business: poiObj["business"],
  255. alpha: otherparam["alpha"],
  256. width: otherparam["width"],
  257. height: otherparam["height"]
  258. }
  259. otherparam.success(outputRes);
  260. } else {
  261. otherparam.fail({
  262. errMsg: res["message"],
  263. statusCode: res["status"]
  264. });
  265. }
  266. },
  267. fail(data) {
  268. otherparam.fail(data);
  269. }
  270. });
  271. };
  272. let locationfail = function(result) {
  273. otherparam.fail(result);
  274. }
  275. let locationcomplete = function(result) {};
  276. if (!param["location"]) {
  277. that.getWXLocation(type, locationsuccess, locationfail, locationcomplete);
  278. } else {
  279. let longitude = param.location.split(',')[1];
  280. let latitude = param.location.split(',')[0];
  281. let errMsg = 'input location';
  282. let res = {
  283. errMsg: errMsg,
  284. latitude: latitude,
  285. longitude: longitude
  286. };
  287. locationsuccess(res);
  288. }
  289. }
  290. /**
  291. * gc检索(地理编码:地点->经纬度)
  292. *
  293. * @param {Object} param 检索配置
  294. * 参数对象结构可以参考
  295. * https://lbs.baidu.com/index.php?title=webapi/guide/webservice-geocoding
  296. *
  297. */
  298. geocoding(param) {
  299. var that = this;
  300. param = param || {};
  301. let geocodingparam = {
  302. address: param["address"] || '',
  303. city: param["city"] || '',
  304. ret_coordtype: param["coordtype"] || 'gcj02ll',
  305. ak: that.ak,
  306. sn: param["sn"] || '',
  307. output: param["output"] || 'json',
  308. callback: param["callback"] || function() {}
  309. };
  310. let otherparam = {
  311. iconPath: param["iconPath"],
  312. iconTapPath: param["iconTapPath"],
  313. width: param["width"],
  314. height: param["height"],
  315. alpha: param["alpha"] || 1,
  316. success: param["success"] || function() {},
  317. fail: param["fail"] || function() {}
  318. };
  319. if (param["address"]) {
  320. wx.request({
  321. url: 'https://api.map.baidu.com/geocoding/v3',
  322. data: geocodingparam,
  323. header: {
  324. "content-type": "application/json"
  325. },
  326. method: 'GET',
  327. success(data) {
  328. let res = data["data"];
  329. if (res["status"] === 0) {
  330. let poiObj = res["result"];
  331. // outputRes 包含两个对象:
  332. // originalData为百度接口返回的原始数据
  333. // wxMarkerData为小程序规范的marker格式
  334. let outputRes = res;
  335. outputRes["originalData"] = res;
  336. outputRes["wxMarkerData"] = [];
  337. outputRes["wxMarkerData"][0] = {
  338. id: 0,
  339. latitude: poiObj["location"]["lat"],
  340. longitude: poiObj["location"]["lng"],
  341. iconPath: otherparam["iconPath"],
  342. iconTapPath: otherparam["iconTapPath"],
  343. alpha: otherparam["alpha"],
  344. width: otherparam["width"],
  345. height: otherparam["height"]
  346. }
  347. otherparam.success(outputRes);
  348. } else {
  349. otherparam.fail({
  350. errMsg: res["message"],
  351. statusCode: res["status"]
  352. });
  353. }
  354. },
  355. fail(data) {
  356. otherparam.fail(data);
  357. }
  358. });
  359. } else {
  360. let errMsg = 'input address!';
  361. let res = {
  362. errMsg: errMsg
  363. };
  364. otherparam.fail(res);
  365. }
  366. }
  367. /**
  368. * 天气检索
  369. *
  370. * @param {Object} param 检索配置
  371. */
  372. weather(param) {
  373. var that = this;
  374. param = param || {};
  375. let weatherparam = {
  376. coord_type: param["coord_type"] || 'gcj02',
  377. output: param["output"] || 'json',
  378. ak: that.ak,
  379. sn: param["sn"] || '',
  380. timestamp: param["timestamp"] || ''
  381. };
  382. let otherparam = {
  383. success: param["success"] || function() {},
  384. fail: param["fail"] || function() {}
  385. };
  386. let type = 'gcj02';
  387. let locationsuccess = function(result) {
  388. weatherparam["location"] = result["longitude"] + ',' + result["latitude"];
  389. wx.request({
  390. url: 'https://api.map.baidu.com/telematics/v3/weather',
  391. data: weatherparam,
  392. header: {
  393. "content-type": "application/json"
  394. },
  395. method: 'GET',
  396. success(data) {
  397. let res = data["data"];
  398. if (res["error"] === 0 && res["status"] === 'success') {
  399. let weatherArr = res["results"];
  400. // outputRes 包含两个对象,
  401. // originalData为百度接口返回的原始数据
  402. // wxMarkerData为小程序规范的marker格式
  403. let outputRes = {};
  404. outputRes["originalData"] = res;
  405. outputRes["currentWeather"] = [];
  406. outputRes["currentWeather"][0] = {
  407. currentCity: weatherArr[0]["currentCity"],
  408. pm25: weatherArr[0]["pm25"],
  409. date: weatherArr[0]["weather_data"][0]["date"],
  410. temperature: weatherArr[0]["weather_data"][0]["temperature"],
  411. weatherDesc: weatherArr[0]["weather_data"][0]["weather"],
  412. wind: weatherArr[0]["weather_data"][0]["wind"]
  413. };
  414. otherparam.success(outputRes);
  415. } else {
  416. otherparam.fail({
  417. errMsg: res["message"],
  418. statusCode: res["status"]
  419. });
  420. }
  421. },
  422. fail(data) {
  423. otherparam.fail(data);
  424. }
  425. });
  426. }
  427. let locationfail = function(result) {
  428. otherparam.fail(result);
  429. }
  430. let locationcomplete = function(result) {}
  431. if (!param["location"]) {
  432. that.getWXLocation(type, locationsuccess, locationfail, locationcomplete);
  433. } else {
  434. let longitude = param.location.split(',')[0];
  435. let latitude = param.location.split(',')[1];
  436. let errMsg = 'input location';
  437. let res = {
  438. errMsg: errMsg,
  439. latitude: latitude,
  440. longitude: longitude
  441. };
  442. locationsuccess(res);
  443. }
  444. }
  445. }
  446. module.exports.BMapWX = BMapWX;