| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- import { params } from "@/env";
- export type Address = { address: string; latlng: number[]; id: string };
- const platform = {
- gaode(val: string) {
- const key = params.mapKey || "3bddec1685d461c2271a6099cde02fd2";
- const url = `https://restapi.amap.com/v3/geocode/geo?address=${encodeURIComponent(
- val
- )}&key=${key}`;
- return fetch(url)
- .then((res) => res.json())
- .then((res) => {
- if (res.info !== "OK") {
- throw res.info;
- }
- console.log(res);
- const items = res.geocodes
- .map((item: any) => ({
- id: item.location,
- address: item.formatted_address,
- latlng: item.location
- .split(",")
- .map((item: string) => Number(item.trim())),
- }))
- .slice(0, 10);
- return items;
- });
- },
- async jm(val: string) {
- const tipParams = new URLSearchParams();
- tipParams.set("basic", "y");
- tipParams.set("key", val);
- tipParams.set("location", "113.05,22.61");
- const keyList = (await fetch(`/s/api/gettips?${tipParams.toString()}`)
- .then((res) => res.json())
- .then((res) => res.data)) as { name: string }[];
- // const keyList = [{name: '港湾一号'},{name: '港湾二号'},]
- const items: Address[] = [];
- const reqs = keyList.map(({ name }) => {
- const params = new URLSearchParams();
- params.set("name", name);
- return fetch(`/s/api/gettips_name?${params.toString()}`)
- .then((res) => res.json())
- .then((res) => res.data)
- .then((data) => {
- items.push({
- latlng: [Number(data.lat), Number(data.lng)],
- address: name,
- id: data.lat.toString() + "," + data.lng.toString(),
- });
- });
- });
- await Promise.all(reqs)
- return items
- },
- };
- export const searchAddress = (val: string): Promise<Address[]> => {
- if (!val) return Promise.resolve([]);
- console.log(import.meta.env.VITE_MAP_PLATFORM)
- const p = (
- params.mapPlatform && params.mapPlatform in platform
- ? params.mapPlatform
- : import.meta.env.VITE_MAP_PLATFORM
- ) as keyof typeof platform;
- return platform[p](val);
- };
|