bmap-wx.js 13 KB

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