photo_server_json_flutter/api_v1/geo.js.old2
2026-02-22 22:25:00 +01:00

75 lines
1.8 KiB
Text

const axios = require('axios');
async function loc(lng, lat) {
const primary = await place(lng, lat);
if (!primary) {
return await place1(lng, lat);
}
if (!primary.county_code) {
const fallback = await place1(lng, lat);
if (fallback && fallback.county_code) {
primary.county_code = fallback.county_code;
}
}
return primary;
}
async function place(lng, lat) {
const apiKey = "6dc7fb95a3b246cfa0f3bcef5ce9ed9a";
const url = `https://api.geoapify.com/v1/geocode/reverse?lat=${lat}&lon=${lng}&apiKey=${apiKey}`;
try {
const r = await axios.get(url);
if (r.status !== 200) return undefined;
if (!r.data.features || r.data.features.length === 0) return undefined;
const k = r.data.features[0].properties;
return {
continent: k?.timezone?.name?.split("/")?.[0] || undefined,
country: k?.country || undefined,
region: k?.state || undefined,
postcode: k?.postcode || undefined,
city: k?.city || undefined,
county_code: k?.county_code || undefined,
address: k?.address_line1 || undefined,
timezone: k?.timezone?.name || undefined,
time: k?.timezone?.offset_STD || undefined
};
} catch (err) {
return undefined;
}
}
async function place1(lng, lat) {
try {
const r = await axios.get(`http://192.168.1.3:6565/query?${lng}&${lat}`);
const k = r.data;
const county = k?.province_code?.includes("-")
? k.province_code.split("-")[1]
: k?.province_code;
return {
continent: k?.region || undefined,
country: k?.country || undefined,
region: undefined,
postcode: undefined,
city: undefined,
county_code: county || undefined,
address: undefined,
timezone: undefined,
time: undefined
};
} catch (err) {
return undefined;
}
}
module.exports = loc;