benlansdell/flying-overhead

JS opensky api call

Opened this issue · 0 comments

Hi, I love the project, Thanks for helping me out bc I'm doing something similar :)
I found how to do the opensky api call from the browser, just leaving it in case anyone is interested

export async function getFlights( coords ) {
 
     // get the user's longitude and latitude
     const [ longitude, latitude ] = [ coords.longitude, coords.latitude ];
 
     // find the min and max longitude and latitude of the search area
     const kmPerDegreeLatitude  = 111.2
     const kmPerDegreeLongitude = 111.2 * Math.cos( latitude / 180 * Math.PI )
     const latitudeDegrees  = SEARCH_DISTANCE / kmPerDegreeLatitude
     const longitudeDegrees = SEARCH_DISTANCE / kmPerDegreeLongitude
 
     // build the query string
     const parameters = {
         lamin: latitude  - latitudeDegrees,
         lomin: longitude - longitudeDegrees,
         lamax: latitude  + latitudeDegrees,
         lomax: longitude + longitudeDegrees
     }
     const queryString = new URLSearchParams(parameters).toString();
 
     // make the request and set the openskyResponse variable
     const response = await fetch( OPENSKY_URL + "?" + queryString, {method: "GET", headers: HEADERS} );
     const openskyResponse = await response.json();

    return openskyResponse;
}