api.js 15 KB

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