()
| 461 | } |
| 462 | |
| 463 | async function useCurrentLocation() { |
| 464 | if (!navigator.geolocation) { |
| 465 | setMessage("当前浏览器不支持定位。"); |
| 466 | return; |
| 467 | } |
| 468 | setSearching(true); |
| 469 | setMessage("正在获取当前位置..."); |
| 470 | navigator.geolocation.getCurrentPosition( |
| 471 | async (position) => { |
| 472 | try { |
| 473 | const { latitude, longitude, accuracy } = position.coords; |
| 474 | const converted = await api.convertGeo({ lat: latitude, lng: longitude }); |
| 475 | const next = { |
| 476 | name: `当前位置 ${new Date().toLocaleTimeString("zh-CN", { hour: "2-digit", minute: "2-digit" })}`, |
| 477 | lat: converted.map_lat, |
| 478 | lng: converted.map_lng, |
| 479 | address: `定位精度约 ${Math.round(accuracy)} 米`, |
| 480 | source: "gps" as const, |
| 481 | }; |
| 482 | applyPlace(next); |
| 483 | setMessage(`已使用当前位置,定位精度约 ${Math.round(accuracy)} 米。`); |
| 484 | } catch (err) { |
| 485 | setMessage(String(err)); |
| 486 | } finally { |
| 487 | setSearching(false); |
| 488 | } |
| 489 | }, |
| 490 | (err) => { |
| 491 | setMessage(`定位失败:${err.message}`); |
| 492 | setSearching(false); |
| 493 | }, |
| 494 | { enableHighAccuracy: true, maximumAge: 5000, timeout: 15000 } |
| 495 | ); |
| 496 | } |
| 497 | |
| 498 | async function create() { |
| 499 | if (!canCreate) { |
nothing calls this directly
no test coverage detected