index.tsx 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import { AMap, PathSimplifier } from '@/components';
  2. import { usePlaceSearch } from '@/components/AMapComponent/hooks';
  3. import { Marker } from 'react-amap';
  4. import { useState } from 'react';
  5. import { Select } from 'antd';
  6. import { debounce } from 'lodash';
  7. export default () => {
  8. const [speed] = useState(100000);
  9. const [markerCenter, setMarkerCenter] = useState<any>({ longitude: 0, latitude: 0 });
  10. const [map, setMap] = useState(null);
  11. const { data, search } = usePlaceSearch(map);
  12. const onSearch = (value: string) => {
  13. search(value);
  14. };
  15. console.log(data);
  16. const [pathData] = useState([
  17. {
  18. name: '线路1',
  19. path: [
  20. [116.405289, 39.904987],
  21. [113.964458, 40.54664],
  22. [111.47836, 41.135964],
  23. [108.949297, 41.670904],
  24. [106.380111, 42.149509],
  25. [103.774185, 42.56996],
  26. [101.135432, 42.930601],
  27. [98.46826, 43.229964],
  28. [95.777529, 43.466798],
  29. [93.068486, 43.64009],
  30. [90.34669, 43.749086],
  31. [87.61792, 43.793308],
  32. ],
  33. },
  34. ]);
  35. return (
  36. <div style={{ position: 'relative' }}>
  37. <AMap
  38. AMapUI
  39. style={{
  40. height: 500,
  41. width: '100%',
  42. }}
  43. onInstanceCreated={setMap}
  44. events={{
  45. click: (e: any) => {
  46. setMarkerCenter({
  47. longitude: e.lnglat.lng,
  48. latitude: e.lnglat.lat,
  49. });
  50. },
  51. }}
  52. >
  53. {markerCenter.longitude ? (
  54. // @ts-ignore
  55. <Marker position={markerCenter} />
  56. ) : null}
  57. <PathSimplifier pathData={pathData}>
  58. <PathSimplifier.PathNavigator
  59. speed={speed}
  60. onCreate={(nav) => {
  61. setTimeout(() => {
  62. nav.pause();
  63. }, 5000);
  64. setTimeout(() => {
  65. nav.resume(); // 恢复
  66. }, 7000);
  67. }}
  68. />
  69. </PathSimplifier>
  70. </AMap>
  71. <div style={{ position: 'absolute', top: 0 }}>
  72. <Select
  73. showSearch
  74. options={data}
  75. filterOption={false}
  76. onSearch={debounce(onSearch, 300)}
  77. style={{ width: 300 }}
  78. onSelect={(key: string, node: any) => {
  79. console.log(key, node);
  80. }}
  81. />
  82. </div>
  83. </div>
  84. );
  85. };