index.ts 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /**
  2. * Author: yang jian fei
  3. * Email: 1294485765@qq.com
  4. * Created Date: Wednesday, September 7th 2022, 11:10:36 am
  5. * Modified By: yang jian fei
  6. * Desc: desc
  7. * Copyright (c) 2022 黑发
  8. */
  9. import { getConfig } from '../base/util';
  10. import { IAddressToPointConfig, IAddressToPointResponse, IPointTransferConfig, IPointTransferResponse } from './type';
  11. let mapLoadResolve;
  12. let mapLoadPromise: Promise<IBMap> = new Promise((resolve) => {
  13. mapLoadResolve = resolve;
  14. });
  15. export { mapLoadResolve };
  16. export const getBMap = () => {
  17. return mapLoadPromise;
  18. };
  19. export function getParamsStrByObject(obj: any) {
  20. let arr: string[] = [];
  21. Object.keys(obj).forEach(key => {
  22. arr.push(key + '=' + obj[key]);
  23. });
  24. return arr.join('&');
  25. }
  26. /**
  27. * 通过坐标获取地址 config和返回值参考百度api
  28. * @param config {IPointToAddressConfig} 默认值 {extensions_poi 1,output: 'json',coordtype: 'wgs84ll',timeout: 60 }
  29. * @param aks {String[]} ; ak不传默认使用map ak
  30. *
  31. * @return {promise} ;
  32. */
  33. export function getAddressByPoints(config: IPointToAddressConfig, aks: string[] = []) {
  34. if (!config.location || !config.location.length) {
  35. console.error('请传入location');
  36. }
  37. const appConfig = getConfig() as IAppConfig;
  38. if (appConfig.ak) {
  39. aks.push(appConfig.ak);
  40. }
  41. return new Promise((resolve, reject) => {
  42. if (aks.length) {
  43. let params = Object.assign({
  44. location: [],
  45. extensions_poi: 1,
  46. output: 'json',
  47. coordtype: 'wgs84ll',
  48. timeout: 60
  49. }, config);
  50. let num: number = 0;
  51. const requestBaidu = () => {
  52. const timer = setTimeout(() => {
  53. reject(new Error('查询超时'));
  54. }, params.timeout * 1000);
  55. const callbackName: string = 'baiduReverse' + Math.floor(Math.random() * 1000000);
  56. params.callback = callbackName;
  57. window[callbackName] = (res: IPointToAddressReponse) => {
  58. clearTimeout(timer);
  59. window[callbackName] = null;
  60. document.getElementById(callbackName)?.remove();
  61. if (!res.status) {
  62. resolve(res.result);
  63. } else {
  64. if (num < aks.length * 4) {
  65. num++;
  66. requestBaidu();
  67. } else {
  68. reject(new Error('查询失败'));
  69. }
  70. }
  71. };
  72. params.ak = aks[num % aks.length];
  73. let script = document.createElement('script');
  74. script.id = callbackName;
  75. script.src = `https://api.map.baidu.com/reverse_geocoding/v3/?${getParamsStrByObject(params)}`;
  76. document.getElementsByTagName('head')[0]?.appendChild(script);
  77. };
  78. requestBaidu();
  79. } else {
  80. console.error('请配置百度ak!');
  81. reject(new Error('请配置百度ak!'));
  82. }
  83. });
  84. }
  85. /**
  86. * 通过地址获取坐标 config和返回值参考百度api
  87. * @param config {IAddressToPointConfig} 默认值 {output: 'json',timeout: 60}
  88. * @param aks {String[]} ; ak不传默认使用map ak
  89. *
  90. * @return {promise} ;
  91. */
  92. export function getPointByAddress(config: IAddressToPointConfig, aks: string[] = []) {
  93. if (!config.address) {
  94. console.error('请传入address');
  95. }
  96. const appConfig = getConfig() as IAppConfig;
  97. if (appConfig.ak) {
  98. aks.push(appConfig.ak);
  99. }
  100. return new Promise((resolve, reject) => {
  101. if (aks.length) {
  102. let params = Object.assign({
  103. address: '',
  104. output: 'json',
  105. timeout: 60
  106. }, config);
  107. let num: number = 0;
  108. const requestBaidu = () => {
  109. const timer = setTimeout(() => {
  110. reject(new Error('查询超时'));
  111. }, params.timeout * 1000);
  112. const callbackName: string = 'baidugeocoding' + Math.floor(Math.random() * 1000000);
  113. params.callback = callbackName;
  114. window[callbackName] = (res: IAddressToPointResponse) => {
  115. clearTimeout(timer);
  116. window[callbackName] = null;
  117. document.getElementById(callbackName)?.remove();
  118. if (!res.status) {
  119. resolve(res.result);
  120. } else {
  121. if (num < aks.length * 4) {
  122. num++;
  123. requestBaidu();
  124. } else {
  125. reject(new Error('查询失败'));
  126. }
  127. }
  128. };
  129. params.ak = aks[num % aks.length];
  130. let script = document.createElement('script');
  131. script.id = callbackName;
  132. script.src = `https://api.map.baidu.com/geocoding/v3/?${getParamsStrByObject(params)}`;
  133. document.getElementsByTagName('head')[0]?.appendChild(script);
  134. };
  135. requestBaidu();
  136. } else {
  137. console.error('请配置百度ak!');
  138. reject(new Error('请配置百度ak!'));
  139. }
  140. });
  141. }
  142. /**
  143. * 坐标转换 config和返回值参考百度api
  144. * @param config {IPointTransferConfig} 默认值 {from: 1,to: 5,timeout: 60}
  145. * @param aks {String[]} ; ak不传默认使用map ak
  146. *
  147. * @return {promise} ;
  148. */
  149. export function getPointsTransfer(config: IPointTransferConfig, aks: string[] = []) {
  150. if (!config.locations || !config.locations.length) {
  151. console.error('请传入locations');
  152. }
  153. const appConfig = getConfig() as IAppConfig;
  154. if (appConfig.ak) {
  155. aks.push(appConfig.ak);
  156. }
  157. return new Promise((resolve, reject) => {
  158. if (aks.length) {
  159. let params = Object.assign({
  160. locations: [],
  161. from: 1,
  162. to: 5,
  163. timeout: 60
  164. }, config);
  165. let num: number = 0;
  166. const requestBaidu = () => {
  167. const timer = setTimeout(() => {
  168. reject(new Error('查询超时'));
  169. }, params.timeout * 1000);
  170. const callbackName: string = 'baidugeoconv' + Math.floor(Math.random() * 1000000);
  171. params.callback = callbackName;
  172. window[callbackName] = (res: IPointTransferResponse) => {
  173. clearTimeout(timer);
  174. window[callbackName] = null;
  175. document.getElementById(callbackName)?.remove();
  176. if (!res.status) {
  177. resolve(res.result);
  178. } else {
  179. if (num < aks.length * 4) {
  180. num++;
  181. requestBaidu();
  182. } else {
  183. reject(new Error('查询失败'));
  184. }
  185. }
  186. };
  187. params.ak = aks[num % aks.length];
  188. params.coords = params.locations.join(';');
  189. let script = document.createElement('script');
  190. script.id = callbackName;
  191. script.src = `https://api.map.baidu.com/geoconv/v1/?${getParamsStrByObject(params)}`;
  192. document.getElementsByTagName('head')[0]?.appendChild(script);
  193. };
  194. requestBaidu();
  195. } else {
  196. console.error('请配置百度ak!');
  197. reject(new Error('请配置百度ak!'));
  198. }
  199. });
  200. }