Repository navigation
Expand file tree
/
Copy pathaddress.js
More file actions
96 lines (73 loc) · 2.51 KB
/
Copy pathaddress.js
File metadata and controls
96 lines (73 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import React from 'react';
import Radar from 'radar-sdk-js';
//const axiosRequest = require("axios");
{
var array = new Array();
}
{
var track = new Array();
}
// pages/api/address.js
export default async function handler(req, res) {
try {
console.log(req.body);
const { lat, long, address, category } = req.body;
//geocode address entered in form
const radarApiKey = ''; // Replace with your Radar API key
const radarApiUrl = `https://api.radar.io/v1/geocode/forward?query=${encodeURIComponent(address)}`;
//api call to radar
const radarApiResponse = await fetch(radarApiUrl, {
method: 'GET',
headers: {
'Authorization': radarApiKey,
},
});
const radarApiData = await radarApiResponse.json();
// Extract coordinates from the Radar API response
const { latitude, longitude } = radarApiData.addresses[0];
console.log('Coordinates:', { latitude, longitude });
//formatting the response to return a json with a list of places
let newarr = getPlaces(lat,long,latitude,longitude,category)
res.status(200).json({ text: "hello" })
//res.status(200).send(newarr);
// Respond with the coordinates or any other data you need
} catch (error) {
console.error('Error calling Radar API:', error);
res.status(500).json({ message: 'Internal Server Error' });
}
}
async function getPlaces(lat,long,lat1,long1,cat){
//const nu = prompt("How many places do you want to search for?:");
//let num = parseInt(nu);
try{
let response = await fetch('https://api.geoapify.com/v2/places?categories='+cat+'&filter=rect:'+long+','+lat+','+long1+','+lat1+'&bias=proximity:'+long+','+lat+'&limit=3&apiKey=')
const places = await response.json();
places.features.forEach((place) => {
if (place.properties !== undefined) {
const { name, address_line2, city, state, country, lat, lon } = place.properties;
if (arraybool(name)) {
array.push(place.properties);
}
}
});
console.log(array);
return array;
}
catch (error) {
console.error('Error fetching data:', error);
// Handle error appropriately
return [];
}
}
function arraybool(name){
if(name==="undefined"){
return false;
}
if(!track.includes(name)){
track.push(name);
return true;
}
else{
return false;
}
}