api.js 19 KB

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