api.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  1. var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
  2. function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }
  3. import forOwn from 'lodash/forOwn';
  4. import isEqual from 'lodash/isEqual';
  5. import isNil from 'lodash/isNil';
  6. //import loadjscssfile from './loadScript';
  7. import APILoader from './APILoader';
  8. export var loadApi = function loadApi() {
  9. var key = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '0325e3d6d69cd56de4980b4f28906fd8';
  10. return new APILoader({
  11. key: key,
  12. useAMapUI: false,
  13. version: '1.4.7',
  14. protocol: 'https'
  15. }).load();
  16. };
  17. export var loadMap = function loadMap(key) {
  18. return new Promise(function (resolve, reject) {
  19. if (window.AMap) {
  20. resolve(window.AMap);
  21. }
  22. loadApi(key).then(function (ret) {
  23. if (window.AMap) {
  24. resolve(window.AMap);
  25. } else {
  26. reject(new Error('window.AMap不存在!'));
  27. }
  28. }).catch(function (error) {
  29. reject(new Error('加载地图错误!' + error.message));
  30. });
  31. });
  32. };
  33. // export const loadApiV1 = async (key = '0325e3d6d69cd56de4980b4f28906fd8') => {
  34. // return await loadjscssfile(
  35. // 'https://webapi.amap.com/maps?v=1.4.7&key=' + key,
  36. // 'js'
  37. // );
  38. // };
  39. // export const loadMapV1 = key => {
  40. // return new Promise((resolve, reject) => {
  41. // if (window.AMap) {
  42. // resolve(window.AMap);
  43. // }
  44. // loadApi(key)
  45. // .then(ret => {
  46. // if (window.AMap) {
  47. // resolve(window.AMap);
  48. // } else {
  49. // reject(new Error('window.AMap不存在!'));
  50. // }
  51. // })
  52. // .catch(error => {
  53. // reject(new Error('加载地图错误!' + error.message));
  54. // });
  55. // });
  56. // };
  57. /**
  58. * [加载插件](https://lbs.amap.com/api/javascript-api/guide/abc/plugins)
  59. * 加载完成后,可以调用:
  60. * var toolbar = new AMap.ToolBar();
  61. * map.addControl(toolbar);
  62. * @param {string} name 插件名或插件数组,如:AMap.ToolBar,['AMap.ToolBar','AMap.Driving']
  63. */
  64. export var loadPlugin = function loadPlugin(name) {
  65. return new Promise(function (resolve, reject) {
  66. if (window.AMap) {
  67. window.AMap.plugin(name, function () {
  68. resolve(true);
  69. });
  70. //是否有加载失败的情况,如果加载失败,怎么reject?
  71. } else {
  72. reject(new Error('地图还未加载!'));
  73. }
  74. });
  75. };
  76. ////////////////////////////////////////////////////////////
  77. // Map
  78. ////////////////////////////////////////////////////////////
  79. export var createMap = function createMap(AMap, dom, options, events) {
  80. var __func__ = 'createMap';
  81. if (!AMap) {
  82. console.log(__func__, 'fail! no AMap!');
  83. return null;
  84. }
  85. if (!dom) {
  86. console.log(__func__, 'fail! no dom!');
  87. return null;
  88. }
  89. var map = new AMap.Map(dom, _extends({}, options || {}));
  90. forOwn(events, function (value, key) {
  91. console.log(__func__, 'event on ' + key);
  92. map.on(key, value);
  93. });
  94. console.log(__func__, 'ok!');
  95. return map;
  96. };
  97. export var commonUpdate = function commonUpdate(entity, newOptions, newEvents, oldOptions, oldEvents, operators) {
  98. var __func__ = arguments.length > 6 && arguments[6] !== undefined ? arguments[6] : 'commonUpdate';
  99. // const __func__ = 'commonUpdate';
  100. if (!entity) {
  101. console.log(__func__, 'fail! no entity!');
  102. return false;
  103. }
  104. // 找到改变的属性集合,包含添加,删除及修改的属性,删除的置为null
  105. var props = {};
  106. if (newOptions != oldOptions) {
  107. oldOptions && forOwn(oldOptions, function (value, key) {
  108. // 找到改变的旧属性,用新属性取代
  109. var newValue = newOptions && newOptions[key];
  110. if (!isEqual(newValue, value)) {
  111. if (!(isNil(newValue) && isNil(value))) props[key] = newValue;
  112. }
  113. });
  114. newOptions && forOwn(newOptions, function (value, key) {
  115. // 找到新加的属性,添加进去
  116. var oldValue = oldOptions && oldOptions[key];
  117. if (isNil(oldValue) && !isNil(value)) {
  118. props[key] = value;
  119. }
  120. });
  121. }
  122. // 找到改变的事件集合,包含添加,删除及修改的事件处理函数,删除的置为null
  123. var events = {};
  124. if (newEvents != oldEvents) {
  125. oldEvents && forOwn(oldEvents, function (value, key) {
  126. // 找到改变的旧属性,用新属性取代
  127. var newValue = newEvents && newEvents[key];
  128. if (!isEqual(newValue, value)) {
  129. if (!(isNil(newValue) && isNil(value))) events[key] = newValue;
  130. }
  131. });
  132. newEvents && forOwn(newEvents, function (value, key) {
  133. // 找到新加的属性,添加进去
  134. var oldValue = oldEvents && oldEvents[key];
  135. if (isNil(oldValue) && !isNil(value)) {
  136. events[key] = value;
  137. }
  138. });
  139. }
  140. // let operators = {
  141. // map: v => entity.setMap(v),
  142. // position: v => entity.setPosition(v),
  143. // offset: v => entity.setOffset(v),
  144. // icon: v => entity.setIcon(v),
  145. // content: v => entity.setContent(v),
  146. // topWhenClick: null,
  147. // bubble: null,
  148. // draggable: v => entity.setDraggable(v),
  149. // raiseOnDrag: null,
  150. // cursor: v => entity.setCursor(v),
  151. // visible: null,
  152. // zIndex: v => entity.setzIndex(v),
  153. // angle: v => entity.setAngle(v),
  154. // autoRotation: null,
  155. // animation: v => entity.setAnimation(v),
  156. // shadow: v => entity.setShadow(v),
  157. // title: v => entity.setTitle(v),
  158. // clickable: v => entity.setClickable(v),
  159. // shape: v => entity.setShape(v),
  160. // extData: v => entity.setExtData(v),
  161. // label: v => entity.setLabel(v)
  162. // };
  163. forOwn(props, function (value, key) {
  164. if (value) {
  165. var func = operators[key];
  166. if (func) {
  167. func(value);
  168. } else {
  169. // ignore properties can not set.
  170. console.log(__func__, 'warning! no setter! can not update ' + key);
  171. }
  172. } else {
  173. // key removed, not support!
  174. console.log(__func__, 'warning! remove prop not support! key=' + key);
  175. }
  176. });
  177. forOwn(events, function (value, key) {
  178. var oldFunc = oldEvents && oldEvents[key];
  179. if (oldFunc) {
  180. entity.off(key, oldFunc);
  181. }
  182. if (value) {
  183. entity.on(key, value);
  184. }
  185. });
  186. console.log(__func__, 'update:', props, events
  187. // newOptions,
  188. // newEvents,
  189. // oldOptions,
  190. // oldEvents
  191. );
  192. return true;
  193. };
  194. export var updateMap = function updateMap(map, newOptions, newEvents, oldOptions, oldEvents) {
  195. var operators = {
  196. view: null,
  197. layers: function layers(v) {
  198. return map.setLayers(v);
  199. },
  200. zoom: function zoom(v) {
  201. return map.setZoom(v);
  202. },
  203. center: function center(v) {
  204. return map.setCenter(v);
  205. },
  206. labelzIndex: function labelzIndex(v) {
  207. return map.setlabelzIndex(v);
  208. },
  209. zooms: null,
  210. lang: function lang(v) {
  211. return map.setLang(v);
  212. },
  213. defaultCursor: function defaultCursor(v) {
  214. return map.setDefaultCursor(v);
  215. },
  216. crs: null,
  217. animateEnable: null,
  218. isHotspot: function isHotspot(v) {
  219. return map.setStatus({ isHotspot: v });
  220. },
  221. defaultLayer: function defaultLayer(v) {
  222. return map.setDefaultLayer(v);
  223. },
  224. rotateEnable: null,
  225. resizeEnable: null,
  226. showIndoorMap: null,
  227. indoorMap: null,
  228. expandZoomRange: null,
  229. dragEnable: function dragEnable(v) {
  230. return map.setStatus({ dragEnable: v });
  231. },
  232. zoomEnable: null,
  233. doubleClickZoom: function doubleClickZoom(v) {
  234. return map.setStatus({ doubleClickZoom: v });
  235. },
  236. keyboardEnable: function keyboardEnable(v) {
  237. return map.setStatus({ keyboardEnable: v });
  238. },
  239. jogEnable: null,
  240. scrollWheel: null,
  241. touchZoom: null,
  242. touchZoomCenter: null,
  243. mapStyle: function mapStyle(v) {
  244. return map.setMapStyle(v);
  245. },
  246. features: function features(v) {
  247. return map.setFeatures(v);
  248. },
  249. showBuildingBlock: null,
  250. viewMode: null,
  251. pitch: function pitch(v) {
  252. return map.setPitch(v);
  253. },
  254. pitchEnable: null,
  255. buildAnimation: null,
  256. skyColor: null,
  257. preloadMode: null
  258. };
  259. return commonUpdate(map, newOptions, newEvents, oldOptions, oldEvents, operators, 'updateMap');
  260. };
  261. ////////////////////////////////////////////////////////////
  262. // Marker
  263. ////////////////////////////////////////////////////////////
  264. /**
  265. *
  266. * @param {*} AMap
  267. * @param {*} map
  268. * @param {*} options 如果有dom用来显示,则其中的content字段即被填充为dom,不再用独立参数表示dom
  269. * @param {*} events
  270. */
  271. export var createMarker = function createMarker(AMap, options, events) {
  272. var __func__ = 'createMarker';
  273. if (!AMap) {
  274. console.log(__func__, 'fail! no AMap!');
  275. return null;
  276. }
  277. if (!options) {
  278. console.log(__func__, 'fail! no options!');
  279. return null;
  280. }
  281. if (!options.map) {
  282. console.log(__func__, 'fail! no options.map!');
  283. return null;
  284. }
  285. // let marker = new AMap.Marker({
  286. // icon: "https://webapi.amap.com/theme/v1.3/markers/n/mark_b.png",
  287. // position: [116.405467, 39.907761]
  288. // });
  289. // marker.setMap(map);
  290. var entity = new AMap.Marker(options);
  291. forOwn(events, function (value, key) {
  292. entity.on(key, value);
  293. });
  294. console.log(__func__, 'ok!');
  295. return entity;
  296. };
  297. export var updateMarker = function updateMarker(entity, newOptions, newEvents, oldOptions, oldEvents) {
  298. var operators = {
  299. map: function map(v) {
  300. return entity.setMap(v);
  301. },
  302. position: function position(v) {
  303. return entity.setPosition(v);
  304. },
  305. offset: function offset(v) {
  306. return entity.setOffset(v);
  307. },
  308. icon: function icon(v) {
  309. return entity.setIcon(v);
  310. },
  311. content: function content(v) {
  312. return entity.setContent(v);
  313. },
  314. topWhenClick: null,
  315. bubble: null,
  316. draggable: function draggable(v) {
  317. return entity.setDraggable(v);
  318. },
  319. raiseOnDrag: null,
  320. cursor: function cursor(v) {
  321. return entity.setCursor(v);
  322. },
  323. visible: null,
  324. zIndex: function zIndex(v) {
  325. return entity.setzIndex(v);
  326. },
  327. angle: function angle(v) {
  328. return entity.setAngle(v);
  329. },
  330. autoRotation: null,
  331. animation: function animation(v) {
  332. return entity.setAnimation(v);
  333. },
  334. shadow: function shadow(v) {
  335. return entity.setShadow(v);
  336. },
  337. title: function title(v) {
  338. return entity.setTitle(v);
  339. },
  340. clickable: function clickable(v) {
  341. return entity.setClickable(v);
  342. },
  343. shape: function shape(v) {
  344. return entity.setShape(v);
  345. },
  346. extData: function extData(v) {
  347. return entity.setExtData(v);
  348. },
  349. label: function label(v) {
  350. return entity.setLabel(v);
  351. }
  352. };
  353. return commonUpdate(entity, newOptions, newEvents, oldOptions, oldEvents, operators, 'updateMarker');
  354. };
  355. ////////////////////////////////////////////////////////////
  356. // MassMarks, warning! is a layer!
  357. ////////////////////////////////////////////////////////////
  358. /**
  359. *
  360. * @param {*} AMap
  361. * @param {*} map
  362. * @param {*} options 如果有dom用来显示,则其中的content字段即被填充为dom,不再用独立参数表示dom
  363. * @param {*} events
  364. */
  365. var createMassMarks = function createMassMarks(AMap, options, events) {
  366. var __func__ = 'createMassMarks';
  367. if (!AMap) {
  368. console.log(__func__, 'fail! no AMap!');
  369. return null;
  370. }
  371. if (!options) {
  372. console.log(__func__, 'fail! no options!');
  373. return null;
  374. }
  375. var map = options.map,
  376. data = options.data,
  377. restOpts = _objectWithoutProperties(options, ['map', 'data']);
  378. var entity = new AMap.MassMarks(data, restOpts);
  379. forOwn(events, function (value, key) {
  380. entity.on(key, value);
  381. });
  382. if (map) {
  383. entity.setMap(map);
  384. }
  385. console.log(__func__, 'ok!');
  386. return entity;
  387. };
  388. export { createMassMarks };
  389. export var updateMassMarks = function updateMassMarks(entity, newOptions, newEvents, oldOptions, oldEvents) {
  390. var operators = {
  391. zIndex: function zIndex(v) {
  392. return entity.setzIndex(v);
  393. },
  394. opacity: null,
  395. zooms: null,
  396. cursor: function cursor(v) {
  397. return entity.setCursor(v);
  398. },
  399. alwaysRender: null,
  400. style: function style(v) {
  401. return entity.setStyle(v);
  402. },
  403. map: function map(v) {
  404. return entity.setMap(v);
  405. },
  406. data: function data(v) {
  407. return entity.setData(v);
  408. }
  409. };
  410. return commonUpdate(entity, newOptions, newEvents, oldOptions, oldEvents, operators, 'updateMassMarks');
  411. };
  412. ////////////////////////////////////////////////////////////
  413. // Polygon
  414. ////////////////////////////////////////////////////////////
  415. /**
  416. *
  417. * @param {*} AMap
  418. * @param {*} map
  419. * @param {*} options 如果有dom用来显示,则其中的content字段即被填充为dom,不再用独立参数表示dom
  420. * @param {*} events
  421. */
  422. export var createPolygon = function createPolygon(AMap, options, events) {
  423. var __func__ = 'createPolygon';
  424. if (!AMap) {
  425. console.log(__func__, 'fail! no AMap!');
  426. return null;
  427. }
  428. if (!options) {
  429. console.log(__func__, 'fail! no options!');
  430. return null;
  431. }
  432. if (!options.map) {
  433. console.log(__func__, 'fail! no options.map!');
  434. return null;
  435. }
  436. var entity = new AMap.Polygon(options);
  437. forOwn(events, function (value, key) {
  438. entity.on(key, value);
  439. });
  440. console.log(__func__, 'ok!');
  441. return entity;
  442. };
  443. export var updatePolygon = function updatePolygon(entity, newOptions, newEvents, oldOptions, oldEvents) {
  444. var operators = {
  445. map: function map(v) {
  446. return entity.setMap(v);
  447. },
  448. zIndex: function zIndex(v) {
  449. return entity.setzIndex(v);
  450. },
  451. path: function path(v) {
  452. return entity.setPath(v);
  453. },
  454. bubble: null,
  455. cursor: null,
  456. strokeColor: null,
  457. strokeOpacity: null,
  458. strokeWeight: null,
  459. fillColor: null,
  460. fillOpacity: null,
  461. draggable: null,
  462. extData: function extData(v) {
  463. return entity.setExtData(v);
  464. },
  465. strokeStyle: null,
  466. strokeDasharray: null,
  467. options: function options(v) {
  468. return entity.setOptions(v);
  469. }
  470. };
  471. return commonUpdate(entity, newOptions, newEvents, oldOptions, oldEvents, operators, 'updatePolygon');
  472. };
  473. ////////////////////////////////////////////////////////////
  474. // Polyline
  475. ////////////////////////////////////////////////////////////
  476. /**
  477. *
  478. * @param {*} AMap
  479. * @param {*} map
  480. * @param {*} options 如果有dom用来显示,则其中的content字段即被填充为dom,不再用独立参数表示dom
  481. * @param {*} events
  482. */
  483. export var createPolyline = function createPolyline(AMap, options, events) {
  484. var __func__ = 'createPolyline';
  485. if (!AMap) {
  486. console.log(__func__, 'fail! no AMap!');
  487. return null;
  488. }
  489. if (!options) {
  490. console.log(__func__, 'fail! no options!');
  491. return null;
  492. }
  493. if (!options.map) {
  494. console.log(__func__, 'fail! no options.map!');
  495. return null;
  496. }
  497. var entity = new AMap.Polyline(options);
  498. forOwn(events, function (value, key) {
  499. entity.on(key, value);
  500. });
  501. console.log(__func__, 'ok!');
  502. return entity;
  503. };
  504. export var updatePolyline = function updatePolyline(entity, newOptions, newEvents, oldOptions, oldEvents) {
  505. var operators = {
  506. map: function map(v) {
  507. return entity.setMap(v);
  508. },
  509. zIndex: function zIndex(v) {
  510. return entity.setzIndex(v);
  511. },
  512. bubble: null,
  513. cursor: null,
  514. gedodesic: null,
  515. isOutline: null,
  516. borderWeight: null,
  517. outlineColor: null,
  518. path: function path(v) {
  519. return entity.setPath(v);
  520. },
  521. strokeColor: null,
  522. strokeOpacity: null,
  523. strokeWeight: null,
  524. strokeStyle: null,
  525. strokeDasharray: null,
  526. lineJoin: null,
  527. lineCap: null,
  528. draggable: null,
  529. extData: function extData(v) {
  530. return entity.setExtData(v);
  531. },
  532. showDir: null,
  533. options: function options(v) {
  534. return entity.setOptions(v);
  535. }
  536. };
  537. return commonUpdate(entity, newOptions, newEvents, oldOptions, oldEvents, operators, 'updatePolyline');
  538. };