bmap-wx.js 15 KB

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