server-json/api_v1/geo.js
2024-11-01 08:00:42 +00:00

55 lines
No EOL
1.9 KiB
JavaScript

const axios = require('axios');
async function loc(lng,lat){
const l = await place(lng,lat);
if(l === undefined){
return await place1(lng,lat);
} else {
if(l.county_code === undefined){
const l1 = await place1(lng,lat);
if(l1 === undefined){ } else {
l.county_code = l1.province_code;
}
}
return l;
}
}
async function place(lng,lat) {
const myAPIKey = "6dc7fb95a3b246cfa0f3bcef5ce9ed9a";
const reverseGeocodingUrl = `https://api.geoapify.com/v1/geocode/reverse?lat=${lat}&lon=${lng}&apiKey=${myAPIKey}`
//console.log(reverseGeocodingUrl);
try {
const f = await axios.get(reverseGeocodingUrl);
if(f.status == 200){
const k = f.data.features[0].properties;
return {continent: k.timezone.name.split("/")[0],country: k.country, region: k.state, postcode: k.postcode, city: k.city, county_code: k.county_code, address: k.address_line1, timezone: k.timezone.name, time: k.timezone.offset_STD }
} else {
//console.log("errore1a");
return undefined;
}
} catch (error) {
//console.log("errore1b");
return undefined;
}
}
async function place1(lng,lat){
try {
const loc = await axios.get('http://192.168.1.3:6565/query?'+lng+'&'+lat);
const k = loc.data
//console.log(loc);
return {continent: k.region,country: k.country, region: undefined, postcode: undefined, city: undefined, county_code: k.province_code.split("-")[1], address: undefined, timezone: undefined, time: undefined };
} catch (error) {
//console.log("errore2");
return undefined;
}
}
module.exports = loc;