File size: 10,111 Bytes
e1dd24e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3f4fb25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e1dd24e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3f4fb25
e1dd24e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3f4fb25
 
 
 
e1dd24e
3f4fb25
 
e1dd24e
 
3f4fb25
e1dd24e
 
 
 
 
 
 
 
 
 
3f4fb25
e1dd24e
 
 
 
 
3f4fb25
e1dd24e
 
 
 
 
 
 
 
 
 
 
3f4fb25
 
 
 
 
 
 
 
 
 
e1dd24e
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <title>地图打卡系统</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.9.4/leaflet.css" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.9.4/leaflet.js"></script>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
        }

        #map {
            width: 100%;
            height: 100vh;
            position: relative;
            z-index: 1;
        }

        .toolbar {
            position: fixed;
            bottom: 20px;
            left: 50%;
            transform: translateX(-50%);
            background: white;
            padding: 10px 20px;
            border-radius: 20px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            display: flex;
            gap: 10px;
            z-index: 1000;
        }

        .button {
            padding: 8px 16px;
            border: none;
            border-radius: 15px;
            background: #4CAF50;
            color: white;
            font-size: 14px;
            cursor: pointer;
            transition: background-color 0.3s;
        }

        .button:hover {
            background: #45a049;
        }

        .checkin-list {
            position: fixed;
            top: 10px;
            right: 10px;
            background: white;
            padding: 10px;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            max-height: 50vh;
            overflow-y: auto;
            z-index: 1000;
        }

        .checkin-item {
            margin-bottom: 8px;
            padding: 8px;
            background: #f5f5f5;
            border-radius: 5px;
            font-size: 12px;
            position: relative;
        }

        .delete-button {
            position: absolute;
            top: 8px;
            right: 8px;
            background: #ff4444;
            color: white;
            border: none;
            border-radius: 3px;
            padding: 2px 6px;
            font-size: 11px;
            cursor: pointer;
        }

        .delete-button:hover {
            background: #ee3333;
        }

        .dialog {
            display: none;
            position: fixed;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            background: white;
            padding: 20px;
            border-radius: 10px;
            box-shadow: 0 2px 20px rgba(0,0,0,0.2);
            z-index: 1000;
            width: 80%;
            max-width: 300px;
        }

        .dialog input {
            width: 100%;
            padding: 8px;
            margin: 10px 0;
            border: 1px solid #ddd;
            border-radius: 5px;
        }

        .dialog-buttons {
            display: flex;
            justify-content: flex-end;
            gap: 10px;
            margin-top: 15px;
        }

        .overlay {
            display: none;
            position: fixed;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background: rgba(0,0,0,0.5);
            z-index: 999;
        }
    </style>
</head>
<body>
    <div id="map"></div>
    
    <div class="toolbar">
        <button class="button" onclick="getCurrentLocation()">定位</button>
        <button class="button" onclick="startCheckin()">打卡</button>
        <button class="button" onclick="toggleCheckinList()">记录</button>
    </div>

    <div class="checkin-list" id="checkinList" style="display: none;">
        <h3>打卡记录</h3>
        <div id="checkinRecords"></div>
    </div>

    <div class="overlay" id="overlay"></div>
    <div class="dialog" id="checkinDialog">
        <h3>添加打卡记录</h3>
        <input type="text" id="checkinNote" placeholder="添加备注...">
        <div class="dialog-buttons">
            <button class="button" onclick="cancelCheckin()">取消</button>
            <button class="button" onclick="saveCheckin()">保存</button>
        </div>
    </div>

    <script>
        let map;
        let marker;
        let checkinLocation;
        let markers = [];
        const STORAGE_KEY = 'mapCheckins';

        // 初始化地图
        function initMap() {
            map = L.map('map').setView([39.9042, 116.4074], 11);
            L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
                attribution: '© OpenStreetMap contributors'
            }).addTo(map);

            // 从本地存储加载打卡记录并显示标记
            loadCheckins();

            // 添加点击事件监听
            map.on('click', function(e) {
                checkinLocation = e.latlng;
                if (marker) {
                    marker.setLatLng(checkinLocation);
                } else {
                    marker = L.marker(checkinLocation).addTo(map);
                }
            });
        }

        // 获取当前位置
        function getCurrentLocation() {
            if (navigator.geolocation) {
                navigator.geolocation.getCurrentPosition(
                    function(position) {
                        const lat = position.coords.latitude;
                        const lng = position.coords.longitude;
                        checkinLocation = L.latLng(lat, lng);
                        
                        map.setView(checkinLocation, 15);
                        
                        if (marker) {
                            marker.setLatLng(checkinLocation);
                        } else {
                            marker = L.marker(checkinLocation).addTo(map);
                        }
                    },
                    function(error) {
                        alert('无法获取位置:' + error.message);
                    },
                    {
                        enableHighAccuracy: true,
                        timeout: 5000,
                        maximumAge: 0
                    }
                );
            } else {
                alert('您的浏览器不支持地理定位');
            }
        }

        // 开始打卡
        function startCheckin() {
            if (!checkinLocation) {
                alert('请先在地图上选择位置或使用定位!');
                return;
            }
            document.getElementById('overlay').style.display = 'block';
            document.getElementById('checkinDialog').style.display = 'block';
        }

        // 取消打卡
        function cancelCheckin() {
            document.getElementById('overlay').style.display = 'none';
            document.getElementById('checkinDialog').style.display = 'none';
            document.getElementById('checkinNote').value = '';
        }

        // 保存打卡记录
        function saveCheckin() {
            const note = document.getElementById('checkinNote').value;
            const checkin = {
                location: {
                    lat: checkinLocation.lat,
                    lng: checkinLocation.lng
                },
                note: note,
                timestamp: new Date().toLocaleString()
            };

            let checkins = JSON.parse(localStorage.getItem(STORAGE_KEY) || '[]');
            checkins.push(checkin);
            localStorage.setItem(STORAGE_KEY, JSON.stringify(checkins));

            // 添加新标记
            L.marker([checkin.location.lat, checkin.location.lng])
                .bindPopup(note)
                .addTo(map);

            cancelCheckin();
            updateCheckinList();
        }

        // 加载已有的打卡记录
        function loadCheckins() {
            // 清除现有标记
            markers.forEach(marker => map.removeLayer(marker));
            markers = [];
            
            const checkins = JSON.parse(localStorage.getItem(STORAGE_KEY) || '[]');
            checkins.forEach((checkin, index) => {
                const marker = L.marker([checkin.location.lat, checkin.location.lng])
                    .bindPopup(checkin.note)
                    .addTo(map);
                markers.push(marker);
            });
            updateCheckinList();
        }

        // 更新打卡记录列表
        function updateCheckinList() {
            const checkins = JSON.parse(localStorage.getItem(STORAGE_KEY) || '[]');
            const recordsContainer = document.getElementById('checkinRecords');
            recordsContainer.innerHTML = '';

            checkins.reverse().forEach((checkin, index) => {
                const item = document.createElement('div');
                item.className = 'checkin-item';
                item.innerHTML = `
                    <div>${checkin.note}</div>
                    <div style="color: #666; font-size: 11px;">${checkin.timestamp}</div>
                    <button class="delete-button" onclick="deleteCheckin(${checkins.length - 1 - index})">删除</button>
                `;
                recordsContainer.appendChild(item);
            });
        }

        // 切换打卡记录列表显示状态
        function toggleCheckinList() {
            const list = document.getElementById('checkinList');
            list.style.display = list.style.display === 'none' ? 'block' : 'none';
        }

        // 删除打卡记录
        function deleteCheckin(index) {
            if (confirm('确定要删除这条记录吗?')) {
                let checkins = JSON.parse(localStorage.getItem(STORAGE_KEY) || '[]');
                checkins.splice(index, 1);
                localStorage.setItem(STORAGE_KEY, JSON.stringify(checkins));
                loadCheckins(); // 重新加载记录和标记
            }
        }

        // 初始化地图
        initMap();
    </script>
</body>
</html>