map.ts 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import { params } from "@/env";
  2. export type Address = { address: string; latlng: number[]; id: string };
  3. const platform = {
  4. gaode(val: string) {
  5. const key = params.mapKey || "3bddec1685d461c2271a6099cde02fd2";
  6. const url = `https://restapi.amap.com/v3/geocode/geo?address=${encodeURIComponent(
  7. val
  8. )}&key=${key}`;
  9. return fetch(url)
  10. .then((res) => res.json())
  11. .then((res) => {
  12. if (res.info !== "OK") {
  13. throw res.info;
  14. }
  15. console.log(res);
  16. const items = res.geocodes
  17. .map((item: any) => ({
  18. id: item.location,
  19. address: item.formatted_address,
  20. latlng: item.location
  21. .split(",")
  22. .map((item: string) => Number(item.trim())),
  23. }))
  24. .slice(0, 10);
  25. return items;
  26. });
  27. },
  28. async jm(val: string) {
  29. const tipParams = new URLSearchParams();
  30. tipParams.set("basic", "y");
  31. tipParams.set("key", val);
  32. tipParams.set("location", "113.05,22.61");
  33. const keyList = (await fetch(`/s/api/gettips?${tipParams.toString()}`)
  34. .then((res) => res.json())
  35. .then((res) => res.data)) as { name: string }[];
  36. // const keyList = [{name: '港湾一号'},{name: '港湾二号'},]
  37. const items: Address[] = [];
  38. const reqs = keyList.map(({ name }) => {
  39. const params = new URLSearchParams();
  40. params.set("name", name);
  41. return fetch(`/s/api/gettips_name?${params.toString()}`)
  42. .then((res) => res.json())
  43. .then((res) => res.data)
  44. .then((data) => {
  45. items.push({
  46. latlng: [Number(data.lat), Number(data.lng)],
  47. address: name,
  48. id: data.lat.toString() + "," + data.lng.toString(),
  49. });
  50. });
  51. });
  52. await Promise.all(reqs)
  53. return items
  54. },
  55. };
  56. export const searchAddress = (val: string): Promise<Address[]> => {
  57. if (!val) return Promise.resolve([]);
  58. console.log(import.meta.env.VITE_MAP_PLATFORM)
  59. const p = (
  60. params.mapPlatform && params.mapPlatform in platform
  61. ? params.mapPlatform
  62. : import.meta.env.VITE_MAP_PLATFORM
  63. ) as keyof typeof platform;
  64. return platform[p](val);
  65. };