Created
November 3, 2021 02:18
-
-
Save steinbring/f5122ca686d8d22b730300af7fb7f3a9 to your computer and use it in GitHub Desktop.
The code behind: https://wisparks-parkslist.joe.workers.dev/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
addEventListener('fetch', event => { | |
event.respondWith(handleRequest(event.request)) | |
}) | |
function getUrlVars(url) { | |
var vars = {}; | |
var parts = url.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) { | |
vars[key] = value; | |
}); | |
return vars; | |
} | |
function deg2rad(deg) { | |
return deg * (Math.PI/180); | |
} | |
function getDistanceFromLatLonInKm(lat1,lon1,lat2,lon2) { | |
var R = 6371; // Radius of the earth in km | |
var dLat = deg2rad(lat2-lat1); // deg2rad below | |
var dLon = deg2rad(lon2-lon1); | |
var a = | |
Math.sin(dLat/2) * Math.sin(dLat/2) + | |
Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * | |
Math.sin(dLon/2) * Math.sin(dLon/2) | |
; | |
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); | |
var d = R * c; // Distance in km | |
return d; | |
} | |
async function handleRequest(request) { | |
// Set the latitude | |
if(typeof getUrlVars(request.url)["latitude"] !== 'undefined'){ | |
latitude = getUrlVars(request.url)["latitude"]; | |
} | |
else{ | |
latitude = request.cf.latitude; | |
} | |
// Set the longitude | |
if(typeof getUrlVars(request.url)["longitude"] !== 'undefined'){ | |
longitude = getUrlVars(request.url)["longitude"]; | |
} | |
else{ | |
longitude = request.cf.longitude; | |
} | |
// Set the property type | |
if(typeof getUrlVars(request.url)["propertytype"] !== 'undefined'){ | |
propertytype = getUrlVars(request.url)["propertytype"]; | |
} | |
else{ | |
propertytype = null; | |
} | |
// API Crap | |
let endpoint = "https://api.wisparks.jws.app/v1/wisconsinParks.json"; | |
const init = { | |
headers: { | |
"content-type": "application/json;charset=UTF-8", | |
}, | |
} | |
const response = await fetch(endpoint,init) | |
const parkContent = await response.json() | |
// Loop through the array | |
for (let i = 0; i < parkContent.length; i++) { | |
// Use the lat/long values to set distance values | |
parkContent[i].distance = getDistanceFromLatLonInKm(parkContent[i].LatLongCoordinates.split(',')[0],parkContent[i].LatLongCoordinates.split(',')[1],latitude,longitude); | |
// Build the Property Type value | |
if(parkContent[i].statePark == 'TRUE') | |
parkContent[i].propertyType = 'State Park' | |
if(parkContent[i].recreationArea == 'TRUE') | |
parkContent[i].propertyType = 'Recreation Area' | |
if(parkContent[i].stateForest == 'TRUE') | |
parkContent[i].propertyType = 'State Forest' | |
} | |
// If they are requesting parks, recreation areas, or forests, limit it to just those | |
let result = null; | |
if(propertytype=='state-parks'){ | |
result = parkContent.filter(park => park.propertyType === 'State Park'); | |
} | |
if(propertytype=='recreation-area'){ | |
result = parkContent.filter(park => park.propertyType === 'Recreation Area'); | |
} | |
if(propertytype=='state-forest'){ | |
result = parkContent.filter(park => park.propertyType === 'State Forest'); | |
} | |
if(propertytype==null){ | |
result = parkContent; | |
} | |
return new Response(JSON.stringify(result, null, 2), { | |
headers: { | |
"content-type": "application/json;charset=UTF-8", | |
"Access-Control-Allow-Origin": "https://*.jws.app", | |
"Access-Control-Allow-Methods": "GET", | |
"Access-Control-Max-Age": "86400", | |
} | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment