map.ts 2.4 KB

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