()
| 420 | } |
| 421 | |
| 422 | async function searchPlaces() { |
| 423 | const keyword = query.trim(); |
| 424 | if (!keyword) { |
| 425 | setMessage("先输入学校、公园、商场或街区名称。"); |
| 426 | return; |
| 427 | } |
| 428 | setSearching(true); |
| 429 | setMessage(""); |
| 430 | try { |
| 431 | const AMap = await loadAmap(config, ["AMap.PlaceSearch"]); |
| 432 | const placeSearch = new AMap.PlaceSearch({ |
| 433 | city: "全国", |
| 434 | pageSize: 8, |
| 435 | pageIndex: 1, |
| 436 | extensions: "base", |
| 437 | }); |
| 438 | const result = await new Promise<any>((resolve, reject) => { |
| 439 | placeSearch.search(keyword, (status: string, data: any) => { |
| 440 | if (status === "complete") resolve(data); |
| 441 | else reject(new Error("地点搜索失败,请换个关键词。")); |
| 442 | }); |
| 443 | }); |
| 444 | const pois = result?.poiList?.pois || []; |
| 445 | const items = pois |
| 446 | .filter((poi: any) => poi.location) |
| 447 | .map((poi: any) => ({ |
| 448 | id: String(poi.id || `${poi.name}-${poi.location.lng}-${poi.location.lat}`), |
| 449 | name: String(poi.name), |
| 450 | address: [poi.cityname, poi.adname, poi.address].filter(Boolean).join(" · "), |
| 451 | lat: Number(poi.location.lat), |
| 452 | lng: Number(poi.location.lng), |
| 453 | })); |
| 454 | setSuggestions(items); |
| 455 | if (items.length === 0) setMessage("没搜到地点,试试更完整的名称。"); |
| 456 | } catch (err) { |
| 457 | setMessage(String(err)); |
| 458 | } finally { |
| 459 | setSearching(false); |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | async function useCurrentLocation() { |
| 464 | if (!navigator.geolocation) { |
no test coverage detected