|
@@ -1,38 +1,71 @@
|
|
|
import { params } from "@/env";
|
|
|
|
|
|
-export type Address = { address: string; latlng: number[], id: string };
|
|
|
+export type Address = { address: string; latlng: number[]; id: string };
|
|
|
const platform = {
|
|
|
gaode(val: string) {
|
|
|
const key = params.mapKey || "3bddec1685d461c2271a6099cde02fd2";
|
|
|
- return fetch(
|
|
|
- `https://restapi.amap.com/v3/geocode/geo?address=${encodeURIComponent(
|
|
|
- val
|
|
|
- )}&key=${key}`
|
|
|
- )
|
|
|
+ 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);
|
|
|
+ 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([])
|
|
|
+ if (!val) return Promise.resolve([]);
|
|
|
+ console.log(import.meta.env.VITE_MAP_PLATFORM)
|
|
|
const p = (
|
|
|
params.mapPlatform && params.mapPlatform in platform
|
|
|
? params.mapPlatform
|
|
|
- : "gaode"
|
|
|
+ : import.meta.env.VITE_MAP_PLATFORM
|
|
|
) as keyof typeof platform;
|
|
|
|
|
|
return platform[p](val);
|