api.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  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 { isEmpty, isNil, isEqual, forOwn } from "lodash";
  4. //import loadjscssfile from './loadScript';
  5. import APILoader from "./APILoader";
  6. var xdebug = console.log;
  7. // const xdebug = () => {};
  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: true,
  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 window.AMap.ToolBar();
  61. * map.addControl(toolbar);
  62. * @param {string} name 插件名或插件数组,如:window.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. // 工具方法
  78. ////////////////////////////////////////////////////////////
  79. var commonUpdate = function commonUpdate(entity, newOptions, newEvents, oldOptions, oldEvents, operators) {
  80. var __func__ = arguments.length > 6 && arguments[6] !== undefined ? arguments[6] : "commonUpdate";
  81. // const __func__ = 'commonUpdate';
  82. if (!entity) {
  83. xdebug(__func__, "fail! no entity!");
  84. return false;
  85. }
  86. // 找到改变的属性集合,包含添加,删除及修改的属性,删除的置为null
  87. var props = {};
  88. if (newOptions != oldOptions) {
  89. oldOptions && forOwn(oldOptions, function (value, key) {
  90. // 找到改变的旧属性,用新属性取代
  91. var newValue = newOptions && newOptions[key];
  92. if (!isEqual(newValue, value)) {
  93. if (!(isNil(newValue) && isNil(value))) props[key] = newValue;
  94. }
  95. });
  96. newOptions && forOwn(newOptions, function (value, key) {
  97. // 找到新加的属性,添加进去
  98. var oldValue = oldOptions && oldOptions[key];
  99. if (isNil(oldValue) && !isNil(value)) {
  100. props[key] = value;
  101. }
  102. });
  103. }
  104. // 找到改变的事件集合,包含添加,删除及修改的事件处理函数,删除的置为null
  105. var events = {};
  106. if (newEvents != oldEvents) {
  107. oldEvents && forOwn(oldEvents, function (value, key) {
  108. // 找到改变的旧属性,用新属性取代
  109. var newValue = newEvents && newEvents[key];
  110. if (!isEqual(newValue, value)) {
  111. if (!(isNil(newValue) && isNil(value))) events[key] = newValue;
  112. }
  113. });
  114. newEvents && forOwn(newEvents, function (value, key) {
  115. // 找到新加的属性,添加进去
  116. var oldValue = oldEvents && oldEvents[key];
  117. if (isNil(oldValue) && !isNil(value)) {
  118. events[key] = value;
  119. }
  120. });
  121. }
  122. // let operators = {
  123. // map: v => entity.setMap(v),
  124. // position: v => entity.setPosition(v),
  125. // offset: v => entity.setOffset(v),
  126. // icon: v => entity.setIcon(v),
  127. // content: v => entity.setContent(v),
  128. // topWhenClick: null,
  129. // bubble: null,
  130. // draggable: v => entity.setDraggable(v),
  131. // raiseOnDrag: null,
  132. // cursor: v => entity.setCursor(v),
  133. // visible: null,
  134. // zIndex: v => entity.setzIndex(v),
  135. // angle: v => entity.setAngle(v),
  136. // autoRotation: null,
  137. // animation: v => entity.setAnimation(v),
  138. // shadow: v => entity.setShadow(v),
  139. // title: v => entity.setTitle(v),
  140. // clickable: v => entity.setClickable(v),
  141. // shape: v => entity.setShape(v),
  142. // extData: v => entity.setExtData(v),
  143. // label: v => entity.setLabel(v)
  144. // };
  145. forOwn(props, function (value, key) {
  146. if (value) {
  147. var func = operators[key];
  148. if (func) {
  149. func(value);
  150. } else {
  151. // ignore properties can not set.
  152. xdebug(__func__, "warning! no setter! can not update " + key);
  153. }
  154. } else {
  155. // key removed, not support!
  156. xdebug(__func__, "warning! remove prop not support! key=" + key);
  157. }
  158. });
  159. forOwn(events, function (value, key) {
  160. var oldFunc = oldEvents && oldEvents[key];
  161. if (oldFunc) {
  162. entity.off(key, oldFunc);
  163. }
  164. if (value) {
  165. entity.on(key, value);
  166. }
  167. });
  168. if (!isEmpty(props) || !isEmpty(events)) {
  169. xdebug(__func__, "update:", entity, props, events
  170. // newOptions,
  171. // newEvents,
  172. // oldOptions,
  173. // oldEvents
  174. );
  175. }
  176. return true;
  177. };
  178. ////////////////////////////////////////////////////////////
  179. // Map
  180. ////////////////////////////////////////////////////////////
  181. export var createMap = function createMap(dom, options, events) {
  182. var __func__ = "createMap";
  183. if (!window.AMap) {
  184. xdebug(__func__, "fail! no window.AMap!");
  185. return null;
  186. }
  187. if (!dom) {
  188. xdebug(__func__, "fail! no dom!");
  189. return null;
  190. }
  191. var map = new window.AMap.Map(dom, _extends({}, options || {}));
  192. forOwn(events, function (value, key) {
  193. xdebug(__func__, "event on " + key);
  194. map.on(key, value);
  195. });
  196. xdebug(__func__, "ok!");
  197. return map;
  198. };
  199. export var updateMap = function updateMap(map, newOptions, newEvents, oldOptions, oldEvents) {
  200. var operators = {
  201. view: null,
  202. layers: function layers(v) {
  203. return map.setLayers(v);
  204. },
  205. zoom: function zoom(v) {
  206. return map.setZoom(v);
  207. },
  208. center: function center(v) {
  209. return map.setCenter(v);
  210. },
  211. labelzIndex: function labelzIndex(v) {
  212. return map.setlabelzIndex(v);
  213. },
  214. zooms: null,
  215. lang: function lang(v) {
  216. return map.setLang(v);
  217. },
  218. defaultCursor: function defaultCursor(v) {
  219. return map.setDefaultCursor(v);
  220. },
  221. crs: null,
  222. animateEnable: null,
  223. isHotspot: function isHotspot(v) {
  224. return map.setStatus({ isHotspot: v });
  225. },
  226. defaultLayer: function defaultLayer(v) {
  227. return map.setDefaultLayer(v);
  228. },
  229. rotateEnable: null,
  230. resizeEnable: null,
  231. showIndoorMap: null,
  232. indoorMap: null,
  233. expandZoomRange: null,
  234. dragEnable: function dragEnable(v) {
  235. return map.setStatus({ dragEnable: v });
  236. },
  237. zoomEnable: null,
  238. doubleClickZoom: function doubleClickZoom(v) {
  239. return map.setStatus({ doubleClickZoom: v });
  240. },
  241. keyboardEnable: function keyboardEnable(v) {
  242. return map.setStatus({ keyboardEnable: v });
  243. },
  244. jogEnable: null,
  245. scrollWheel: null,
  246. touchZoom: null,
  247. touchZoomCenter: null,
  248. mapStyle: function mapStyle(v) {
  249. return map.setMapStyle(v);
  250. },
  251. features: function features(v) {
  252. return map.setFeatures(v);
  253. },
  254. showBuildingBlock: null,
  255. viewMode: null,
  256. pitch: function pitch(v) {
  257. return map.setPitch(v);
  258. },
  259. pitchEnable: null,
  260. buildAnimation: null,
  261. skyColor: null,
  262. preloadMode: null
  263. };
  264. return commonUpdate(map, newOptions, newEvents, oldOptions, oldEvents, operators, "updateMap");
  265. };
  266. ////////////////////////////////////////////////////////////
  267. // Marker
  268. ////////////////////////////////////////////////////////////
  269. /**
  270. *
  271. * @param {*} AMap
  272. * @param {*} map
  273. * @param {*} options 如果有dom用来显示,则其中的content字段即被填充为dom,不再用独立参数表示dom
  274. * @param {*} events
  275. */
  276. export var createMarker = function createMarker(options, events) {
  277. var __func__ = "createMarker";
  278. if (!window.AMap) {
  279. xdebug(__func__, "fail! no window.AMap!");
  280. return null;
  281. }
  282. if (!options) {
  283. xdebug(__func__, "fail! no options!");
  284. return null;
  285. }
  286. if (!options.map) {
  287. xdebug(__func__, "fail! no options.map!");
  288. return null;
  289. }
  290. // let marker = new AMap.Marker({
  291. // icon: "https://webapi.amap.com/theme/v1.3/markers/n/mark_b.png",
  292. // position: [116.405467, 39.907761]
  293. // });
  294. // marker.setMap(map);
  295. var entity = new window.AMap.Marker(options);
  296. forOwn(events, function (value, key) {
  297. entity.on(key, value);
  298. });
  299. xdebug(__func__, "ok!");
  300. return entity;
  301. };
  302. export var updateMarker = function updateMarker(entity, newOptions, newEvents, oldOptions, oldEvents) {
  303. var operators = {
  304. map: function map(v) {
  305. return entity.setMap(v);
  306. },
  307. position: function position(v) {
  308. return entity.setPosition(v);
  309. },
  310. offset: function offset(v) {
  311. return entity.setOffset(v);
  312. },
  313. icon: function icon(v) {
  314. setTimeout(function () {
  315. return entity.setIcon(v);
  316. }, 0);
  317. }, // 这里有一个bug,在某些时候页面更改icon时,界面上并不生效.
  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. // Traffic layer, 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 createTraffic = function createTraffic(options, events) {
  373. var __func__ = "createTraffic";
  374. if (!window.AMap || !options || !options.map) {
  375. xdebug(__func__, "fail! parameters!", "window.AMap:" + !!window.AMap, "options:" + !!options, "options.map:" + !!(options && options.map));
  376. return null;
  377. }
  378. var map = options.map,
  379. restOpts = _objectWithoutProperties(options, ["map"]);
  380. var entity = new window.AMap.TileLayer.Traffic(restOpts);
  381. forOwn(events, function (value, key) {
  382. entity.on(key, value);
  383. });
  384. entity.setMap(map);
  385. xdebug(__func__, "ok!");
  386. return entity;
  387. };
  388. export { createTraffic };
  389. export var updateTraffic = function updateTraffic(entity, newOptions, newEvents, oldOptions, oldEvents) {
  390. var operators = {
  391. map: function map(v) {
  392. return entity.setMap(v);
  393. },
  394. zIndex: function zIndex(v) {
  395. return entity.setzIndex(v);
  396. },
  397. opacity: function opacity(v) {
  398. return entity.setOpacity(v);
  399. },
  400. zooms: null,
  401. detectRetina: null,
  402. autoRefresh: null,
  403. interval: null,
  404. tileUrl: function tileUrl(v) {
  405. return entity.setTileUrl(v);
  406. } // not in options.
  407. };
  408. return commonUpdate(entity, newOptions, newEvents, oldOptions, oldEvents, operators, "updateTraffic");
  409. };
  410. ////////////////////////////////////////////////////////////
  411. // MassMarks, warning! is a layer!
  412. ////////////////////////////////////////////////////////////
  413. /**
  414. *
  415. * @param {*} AMap
  416. * @param {*} map
  417. * @param {*} options 如果有dom用来显示,则其中的content字段即被填充为dom,不再用独立参数表示dom
  418. * @param {*} events
  419. */
  420. var createMassMarks = function createMassMarks(options, events) {
  421. var __func__ = "createMassMarks";
  422. if (!window.AMap || !options || !options.map) {
  423. xdebug(__func__, "fail! parameters!", "window.AMap:" + !!window.AMap, "options:" + !!options, "options.map:" + !!(options && options.map));
  424. return null;
  425. }
  426. var map = options.map,
  427. data = options.data,
  428. style = options.style,
  429. restOpts = _objectWithoutProperties(options, ["map", "data", "style"]);
  430. var entity = new window.AMap.MassMarks(data || [], _extends({}, restOpts, {
  431. style: style || []
  432. }));
  433. forOwn(events, function (value, key) {
  434. entity.on(key, value);
  435. });
  436. entity.setMap(map);
  437. xdebug(__func__, "ok!", map, "layers:", map.getLayers());
  438. return entity;
  439. };
  440. export { createMassMarks };
  441. export var updateMassMarks = function updateMassMarks(entity, newOptions, newEvents, oldOptions, oldEvents) {
  442. var operators = {
  443. zIndex: function zIndex(v) {
  444. return entity.setzIndex(v);
  445. },
  446. opacity: null,
  447. zooms: null,
  448. cursor: function cursor(v) {
  449. return entity.setCursor(v);
  450. },
  451. alwaysRender: null,
  452. style: function style(v) {
  453. return entity.setStyle(v);
  454. },
  455. map: function map(v) {
  456. xdebug("updateMassMarks", "setMap", v, "layers:", v && v.getLayers());
  457. entity.setMap(v);
  458. },
  459. data: function data(v) {
  460. entity.setData(v);
  461. }
  462. };
  463. xdebug("updateMassMarks", "mapOld:", oldOptions && oldOptions.map && oldOptions.map.getLayers(), "mapNew:", newOptions && newOptions.map && newOptions.map.getLayers());
  464. return commonUpdate(entity, newOptions, newEvents, oldOptions, oldEvents, operators, "updateMassMarks");
  465. };
  466. ////////////////////////////////////////////////////////////
  467. // Polygon
  468. ////////////////////////////////////////////////////////////
  469. /**
  470. *
  471. * @param {*} AMap
  472. * @param {*} map
  473. * @param {*} options 如果有dom用来显示,则其中的content字段即被填充为dom,不再用独立参数表示dom
  474. * @param {*} events
  475. */
  476. export var createPolygon = function createPolygon(options, events) {
  477. var __func__ = "createPolygon";
  478. if (!window.AMap || !options || !options.map) {
  479. xdebug(__func__, "fail! parameters!", "window.AMap:" + !!window.AMap, "options:" + !!options, "options.map:" + !!(options && options.map));
  480. return null;
  481. }
  482. var entity = new window.AMap.Polygon(options);
  483. forOwn(events, function (value, key) {
  484. entity.on(key, value);
  485. });
  486. xdebug(__func__, "ok!");
  487. return entity;
  488. };
  489. export var updatePolygon = function updatePolygon(entity, newOptions, newEvents, oldOptions, oldEvents) {
  490. var operators = {
  491. map: function map(v) {
  492. return entity.setMap(v);
  493. },
  494. zIndex: function zIndex(v) {
  495. return entity.setzIndex(v);
  496. },
  497. path: function path(v) {
  498. return entity.setPath(v);
  499. },
  500. bubble: null,
  501. cursor: null,
  502. strokeColor: null,
  503. strokeOpacity: null,
  504. strokeWeight: null,
  505. fillColor: null,
  506. fillOpacity: null,
  507. draggable: null,
  508. extData: function extData(v) {
  509. return entity.setExtData(v);
  510. },
  511. strokeStyle: null,
  512. strokeDasharray: null,
  513. options: function options(v) {
  514. return entity.setOptions(v);
  515. }
  516. };
  517. return commonUpdate(entity, newOptions, newEvents, oldOptions, oldEvents, operators, "updatePolygon");
  518. };
  519. ////////////////////////////////////////////////////////////
  520. // Circle
  521. ////////////////////////////////////////////////////////////
  522. /**
  523. *
  524. * @param {*} AMap
  525. * @param {*} map
  526. * @param {*} options 如果有dom用来显示,则其中的content字段即被填充为dom,不再用独立参数表示dom
  527. * @param {*} events
  528. */
  529. export var createCircle = function createCircle(options, events) {
  530. var __func__ = "createCircle";
  531. if (!window.AMap || !options || !options.map) {
  532. xdebug(__func__, "fail! parameters!", "window.AMap:" + !!window.AMap, "options:" + !!options, "options.map:" + !!(options && options.map));
  533. return null;
  534. }
  535. var entity = new window.AMap.Circle(options);
  536. forOwn(events, function (value, key) {
  537. entity.on(key, value);
  538. });
  539. xdebug(__func__, "ok!");
  540. return entity;
  541. };
  542. export var updateCircle = function updateCircle(entity, newOptions, newEvents, oldOptions, oldEvents) {
  543. var operators = {
  544. map: function map(v) {
  545. return entity.setMap(v);
  546. },
  547. zIndex: function zIndex(v) {
  548. return entity.setzIndex(v);
  549. },
  550. center: function center(v) {
  551. return entity.setCenter(v);
  552. },
  553. bubble: null,
  554. cursor: null,
  555. radius: function radius(v) {
  556. return entity.setRadius(v);
  557. },
  558. strokeColor: null,
  559. strokeOpacity: null,
  560. strokeWeight: null,
  561. fillColor: null,
  562. fillOpacity: null,
  563. strokeStyle: null,
  564. extData: function extData(v) {
  565. return entity.setExtData(v);
  566. },
  567. strokeDasharray: null,
  568. options: function options(v) {
  569. return entity.setOptions(v);
  570. }
  571. };
  572. return commonUpdate(entity, newOptions, newEvents, oldOptions, oldEvents, operators, "updateCircle");
  573. };
  574. ////////////////////////////////////////////////////////////
  575. // Polyline
  576. ////////////////////////////////////////////////////////////
  577. /**
  578. *
  579. * @param {*} AMap
  580. * @param {*} map
  581. * @param {*} options 如果有dom用来显示,则其中的content字段即被填充为dom,不再用独立参数表示dom
  582. * @param {*} events
  583. */
  584. export var createPolyline = function createPolyline(options, events) {
  585. var __func__ = "createPolyline";
  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.Polyline(options);
  591. forOwn(events, function (value, key) {
  592. entity.on(key, value);
  593. });
  594. xdebug(__func__, "ok!");
  595. return entity;
  596. };
  597. export var updatePolyline = function updatePolyline(entity, newOptions, newEvents, oldOptions, oldEvents) {
  598. var operators = {
  599. map: function map(v) {
  600. return entity.setMap(v);
  601. },
  602. zIndex: function zIndex(v) {
  603. return entity.setzIndex(v);
  604. },
  605. bubble: null,
  606. cursor: null,
  607. gedodesic: null,
  608. isOutline: null,
  609. borderWeight: null,
  610. outlineColor: null,
  611. path: function path(v) {
  612. return entity.setPath(v);
  613. },
  614. strokeColor: null,
  615. strokeOpacity: null,
  616. strokeWeight: null,
  617. strokeStyle: null,
  618. strokeDasharray: null,
  619. lineJoin: null,
  620. lineCap: null,
  621. draggable: null,
  622. extData: function extData(v) {
  623. return entity.setExtData(v);
  624. },
  625. showDir: null,
  626. options: function options(v) {
  627. return entity.setOptions(v);
  628. }
  629. };
  630. return commonUpdate(entity, newOptions, newEvents, oldOptions, oldEvents, operators, "updatePolyline");
  631. };
  632. ////////////////////////////////////////////////////////////
  633. // InfoWindow
  634. ////////////////////////////////////////////////////////////
  635. /**
  636. *
  637. * @param {*} AMap
  638. * @param {*} map
  639. * @param {*} options 如果有dom用来显示,则其中的content字段即被填充为dom,不再用独立参数表示dom
  640. * @param {*} events
  641. */
  642. export var createInfoWindow = function createInfoWindow(options, events) {
  643. var __func__ = "createInfoWindow";
  644. if (!window.AMap || !options || !options.map) {
  645. xdebug(__func__, "fail! parameters!", "window.AMap:" + !!window.AMap, "options:" + !!options, "options.map:" + !!(options && options.map));
  646. return null;
  647. }
  648. var entity = new window.AMap.InfoWindow(options);
  649. forOwn(events, function (value, key) {
  650. entity.on(key, value);
  651. });
  652. xdebug(__func__, "ok!");
  653. return entity;
  654. };
  655. export var updateInfoWindow = function updateInfoWindow(entity, newOptions, newEvents, oldOptions, oldEvents) {
  656. var operators = {
  657. isCustom: null,
  658. autoMove: null,
  659. closeWhenClickMap: null,
  660. content: function content(v) {
  661. return entity.setContent(v);
  662. },
  663. size: function size(v) {
  664. return entity.setSize(v);
  665. },
  666. offset: null,
  667. position: function position(v) {
  668. return entity.setPosition(v);
  669. },
  670. showShadow: null
  671. };
  672. return commonUpdate(entity, newOptions, newEvents, oldOptions, oldEvents, operators, "updateInfoWindow");
  673. };
  674. ////////////////////////////////////////////////////////////
  675. // Geolocation
  676. ////////////////////////////////////////////////////////////
  677. export var getLocation = function getLocation(options, events) {
  678. xdebug("准备获取地理位置");
  679. /***************************************
  680. 由于Chrome、IOS10等已不再支持非安全域的浏览器定位请求,为保证定位成功率和精度,请尽快升级您的站点到HTTPS。
  681. ***************************************/
  682. var map = options.map;
  683. if (!map) {
  684. return false;
  685. }
  686. var geolocation = void 0;
  687. var defaultOptions = {
  688. enableHighAccuracy: true, //是否使用高精度定位,默认:true
  689. timeout: 10000, //超过10秒后停止定位,默认:无穷大
  690. buttonOffset: new window.AMap.Pixel(10, 20), //定位按钮与设置的停靠位置的偏移量,默认:Pixel(10, 20)
  691. zoomToAccuracy: true, //定位成功后调整地图视野范围使定位位置及精度范围视野内可见,默认:false
  692. buttonPosition: "RB"
  693. };
  694. var opts = _extends({}, defaultOptions, options);
  695. xdebug("准备加载定位插件");
  696. map.plugin("AMap.Geolocation", function () {
  697. xdebug("加载定位插件成功");
  698. geolocation = new window.AMap.Geolocation(opts);
  699. map.addControl(geolocation);
  700. geolocation.getCurrentPosition();
  701. if (events.onComplete) {
  702. window.AMap.event.addListener(geolocation, "complete", events.onComplete); //返回定位信息
  703. }
  704. if (events.onError) {
  705. window.AMap.event.addListener(geolocation, "error", events.onError); //返回定位出错信息
  706. }
  707. });
  708. return true;
  709. };