api.js 17 KB

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