react-swipe-map 1.0.1 → 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +210 -59
- package/dist/index.es.js +25645 -24042
- package/dist/index.umd.js +240 -85
- package/dist/style.css +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,73 +1,224 @@
|
|
|
1
|
-
#
|
|
1
|
+
# react-swipe-map
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
一个基于 React 和 Leaflet 的卷帘地图组件,支持左右图层对比展示,适用于地图数据对比、变化检测等场景。
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
## ✨ 特性
|
|
6
6
|
|
|
7
|
-
-
|
|
8
|
-
-
|
|
7
|
+
- 🗺️ 基于 Leaflet 的卷帘地图组件
|
|
8
|
+
- 🔄 支持卷帘模式和普通地图模式切换
|
|
9
|
+
- 📍 支持左右图层独立管理
|
|
10
|
+
- 🎨 支持自定义控件图标
|
|
11
|
+
- 🗂️ 支持底图切换
|
|
12
|
+
- 📦 开箱即用,简单易用
|
|
9
13
|
|
|
10
|
-
##
|
|
14
|
+
## 📦 安装
|
|
11
15
|
|
|
12
|
-
|
|
16
|
+
```bash
|
|
17
|
+
npm install react-swipe-map
|
|
18
|
+
# 或
|
|
19
|
+
yarn add react-swipe-map
|
|
20
|
+
# 或
|
|
21
|
+
pnpm add react-swipe-map
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## 🚀 快速开始
|
|
13
25
|
|
|
14
|
-
|
|
26
|
+
### 基本使用
|
|
15
27
|
|
|
16
|
-
|
|
28
|
+
```tsx
|
|
29
|
+
import React, { useRef, useState } from 'react';
|
|
30
|
+
import L from 'leaflet';
|
|
31
|
+
import 'leaflet/dist/leaflet.css';
|
|
32
|
+
import Map from 'react-swipe-map';
|
|
33
|
+
import { Button } from 'antd';
|
|
17
34
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
{
|
|
22
|
-
files: ['**/*.{ts,tsx}'],
|
|
23
|
-
extends: [
|
|
24
|
-
// Other configs...
|
|
35
|
+
function App() {
|
|
36
|
+
const [isRollMap, setIsRollMap] = useState(false);
|
|
37
|
+
const mapRef = useRef(null);
|
|
25
38
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
39
|
+
// 在左侧图层上添加 marker
|
|
40
|
+
const addMarkerToLeft = () => {
|
|
41
|
+
if (!mapRef.current) return;
|
|
42
|
+
const marker = L.marker([30.5984, 114.3118], {
|
|
43
|
+
pane: 'leftPane',
|
|
44
|
+
});
|
|
45
|
+
(mapRef.current as any).addLeftLayers([marker]);
|
|
46
|
+
};
|
|
32
47
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
48
|
+
// 在右侧图层上添加 GeoJSON
|
|
49
|
+
const addGeoJsonToRight = () => {
|
|
50
|
+
if (!mapRef.current) return;
|
|
51
|
+
const geojson = L.geoJSON(
|
|
52
|
+
{
|
|
53
|
+
type: 'Feature',
|
|
54
|
+
geometry: {
|
|
55
|
+
type: 'Polygon',
|
|
56
|
+
coordinates: [
|
|
57
|
+
[
|
|
58
|
+
[115.796, 40.4934],
|
|
59
|
+
[116.377, 40.3945],
|
|
60
|
+
[116.236, 39.9146],
|
|
61
|
+
[115.659, 40.0129],
|
|
62
|
+
[115.796, 40.4934],
|
|
63
|
+
],
|
|
64
|
+
],
|
|
65
|
+
},
|
|
66
|
+
},
|
|
67
|
+
{
|
|
68
|
+
pane: 'rightPane',
|
|
69
|
+
style: {
|
|
70
|
+
color: 'red',
|
|
71
|
+
weight: 3,
|
|
72
|
+
opacity: 0.5,
|
|
73
|
+
fillColor: 'red',
|
|
74
|
+
fillOpacity: 0.2,
|
|
75
|
+
},
|
|
39
76
|
},
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
77
|
+
).addTo((mapRef.current as any).getRightLayersGroup());
|
|
78
|
+
|
|
79
|
+
// 定位到 geojson
|
|
80
|
+
((mapRef.current as any).getMapRef() as L.Map).fitBounds(geojson.getBounds());
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
return (
|
|
84
|
+
<div style={{ height: '100vh', width: '100%' }}>
|
|
85
|
+
<Map
|
|
86
|
+
ref={mapRef}
|
|
87
|
+
leftLayers={[]}
|
|
88
|
+
rightLayers={[]}
|
|
89
|
+
onIsRollMapChange={setIsRollMap}
|
|
90
|
+
/>
|
|
91
|
+
<div style={{ position: 'absolute', top: 10, right: 10, zIndex: 1000 }}>
|
|
92
|
+
<Button onClick={addMarkerToLeft} disabled={!isRollMap}>
|
|
93
|
+
添加 marker 到左侧图层
|
|
94
|
+
</Button>
|
|
95
|
+
<Button onClick={addGeoJsonToRight} disabled={!isRollMap} style={{ marginLeft: 8 }}>
|
|
96
|
+
添加 GeoJSON 到右侧图层
|
|
97
|
+
</Button>
|
|
98
|
+
</div>
|
|
99
|
+
</div>
|
|
100
|
+
);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export default App;
|
|
44
104
|
```
|
|
45
105
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
106
|
+
## 📖 API 文档
|
|
107
|
+
|
|
108
|
+
### Props
|
|
109
|
+
|
|
110
|
+
| 参数 | 说明 | 类型 | 默认值 | 必填 |
|
|
111
|
+
|------|------|------|--------|------|
|
|
112
|
+
| `leftLayers` | 左侧图层列表 | `L.Layer[]` | `[]` | 否 |
|
|
113
|
+
| `rightLayers` | 右侧图层列表 | `L.Layer[]` | `[]` | 否 |
|
|
114
|
+
| `mapSettings` | 地图配置 | `{ center: number[], zoom: number }` | `{ center: [35.8617, 104.1954], zoom: 4 }` | 否 |
|
|
115
|
+
| `rollIcon` | 卷帘模式图标 | `any` | 内置图标 | 否 |
|
|
116
|
+
| `normalIcon` | 普通模式图标 | `any` | 内置图标 | 否 |
|
|
117
|
+
| `baseMapSelectIcon` | 底图选择图标 | `any` | 内置图标 | 否 |
|
|
118
|
+
| `baseMapList` | 底图列表 | `BaseMapConfig[]` | `[]` | 否 |
|
|
119
|
+
| `onIsRollMapChange` | 卷帘模式变化回调 | `(isRollMap: boolean) => void` | - | 否 |
|
|
120
|
+
|
|
121
|
+
### BaseMapConfig
|
|
122
|
+
|
|
123
|
+
```typescript
|
|
124
|
+
interface BaseMapConfig {
|
|
125
|
+
id: string;
|
|
126
|
+
name: string;
|
|
127
|
+
mapUrl: string;
|
|
128
|
+
type: '4326' | '3857';
|
|
129
|
+
centre?: [number, number];
|
|
130
|
+
zoom?: number;
|
|
131
|
+
zoomOffset?: number;
|
|
132
|
+
maxZoom?: number;
|
|
133
|
+
checked?: boolean;
|
|
134
|
+
icon?: string;
|
|
135
|
+
}
|
|
73
136
|
```
|
|
137
|
+
|
|
138
|
+
### Ref Methods
|
|
139
|
+
|
|
140
|
+
通过 `ref` 可以访问以下方法:
|
|
141
|
+
|
|
142
|
+
| 方法名 | 说明 | 类型 |
|
|
143
|
+
|--------|------|------|
|
|
144
|
+
| `addLeftLayers` | 添加左侧图层 | `(layers: L.Layer[]) => void` |
|
|
145
|
+
| `addRightLayers` | 添加右侧图层 | `(layers: L.Layer[]) => void` |
|
|
146
|
+
| `clearLeftLayers` | 清除左侧图层 | `() => void` |
|
|
147
|
+
| `clearRightLayers` | 清除右侧图层 | `() => void` |
|
|
148
|
+
| `getMapInstance` | 获取地图实例 | `() => L.Map \| null` |
|
|
149
|
+
| `toggleRollMap` | 切换卷帘模式 | `(enabled: boolean) => void` |
|
|
150
|
+
| `getIsRollMap` | 获取当前是否为卷帘模式 | `() => boolean` |
|
|
151
|
+
| `getLeftLayersGroup` | 获取左侧图层组 | `() => L.LayerGroup \| null` |
|
|
152
|
+
| `getRightLayersGroup` | 获取右侧图层组 | `() => L.LayerGroup \| null` |
|
|
153
|
+
| `getMapRef` | 获取地图引用 | `() => L.Map \| null` |
|
|
154
|
+
|
|
155
|
+
## 🎯 使用场景
|
|
156
|
+
|
|
157
|
+
- 🗺️ **地图数据对比**:对比不同时期的地图数据
|
|
158
|
+
- 📍 **变化检测**:检测地理数据的变化情况
|
|
159
|
+
- 🎨 **图层对比**:对比不同图层的数据差异
|
|
160
|
+
- 📊 **数据分析**:辅助地理数据分析和可视化
|
|
161
|
+
|
|
162
|
+
## 💡 注意事项
|
|
163
|
+
|
|
164
|
+
1. **Leaflet 样式**:使用前需要引入 Leaflet 的 CSS 文件:
|
|
165
|
+
```tsx
|
|
166
|
+
import 'leaflet/dist/leaflet.css';
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
2. **Pane 设置**:添加图层时,可以通过 `pane` 参数指定图层所属的 pane:
|
|
170
|
+
- `leftPane`:左侧图层
|
|
171
|
+
- `rightPane`:右侧图层
|
|
172
|
+
|
|
173
|
+
3. **卷帘模式**:只有在卷帘模式下,左右图层才会分开显示
|
|
174
|
+
|
|
175
|
+
4. **图层管理**:使用 `addLeftLayers` 和 `addRightLayers` 方法添加图层时,会自动更新卷帘控制器
|
|
176
|
+
|
|
177
|
+
## 🔧 依赖
|
|
178
|
+
|
|
179
|
+
- React >= 18.2.0
|
|
180
|
+
- React DOM >= 18.2.0
|
|
181
|
+
- Leaflet >= 1.9.4
|
|
182
|
+
- leaflet-side-by-side >= 2.2.0
|
|
183
|
+
- antd >= 6.2.3
|
|
184
|
+
|
|
185
|
+
## 📝 更新日志
|
|
186
|
+
|
|
187
|
+
### v1.0.2 (当前版本)
|
|
188
|
+
|
|
189
|
+
- ✨ 初始版本发布
|
|
190
|
+
- 🗺️ 支持卷帘地图基本功能
|
|
191
|
+
- 🔄 支持卷帘模式和普通地图模式切换
|
|
192
|
+
- 📍 支持左右图层独立管理
|
|
193
|
+
- 🎨 支持自定义控件图标
|
|
194
|
+
- 🗂️ 支持底图切换
|
|
195
|
+
|
|
196
|
+
## 🚧 后续优化计划
|
|
197
|
+
|
|
198
|
+
- [ ] 添加更多地图控件(比例尺、鹰眼图等)
|
|
199
|
+
- [ ] 支持更多地图服务(WMS、WFS 等)
|
|
200
|
+
- [ ] 优化性能,减少不必要的渲染
|
|
201
|
+
- [ ] 添加更多示例和文档
|
|
202
|
+
- [ ] 支持 TypeScript 类型定义优化
|
|
203
|
+
- [ ] 添加单元测试
|
|
204
|
+
- [ ] 支持自定义主题
|
|
205
|
+
- [ ] 支持移动端适配
|
|
206
|
+
|
|
207
|
+
## 🤝 贡献
|
|
208
|
+
|
|
209
|
+
欢迎提交 Issue 和 Pull Request!
|
|
210
|
+
|
|
211
|
+
## 📄 许可证
|
|
212
|
+
|
|
213
|
+
MIT License
|
|
214
|
+
|
|
215
|
+
## 📮 联系方式
|
|
216
|
+
|
|
217
|
+
如有问题或建议,请通过以下方式联系:
|
|
218
|
+
|
|
219
|
+
- 提交 Issue:[GitHub Issues](https://github.com/your-username/react-swipe-map/issues)
|
|
220
|
+
- 邮件:your-email@example.com
|
|
221
|
+
|
|
222
|
+
---
|
|
223
|
+
|
|
224
|
+
**⭐ 如果这个项目对你有帮助,请给一个 Star 支持一下!**
|