Created
November 3, 2021 01:08
-
-
Save steinbring/bd36768a61adc016a30d20ae48ffc712 to your computer and use it in GitHub Desktop.
The code behind: https://wisparks-getlocation.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; | |
} | |
async function handleRequest(request) { | |
// Is it a get request? | |
if(request.method == 'GET'){ | |
// 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; | |
} | |
// API Crap | |
let endpoint = "https://www.mapquestapi.com/geocoding/v1/reverse?key=(** API KEY GOES HERE **)&location="+latitude+"%2C"+longitude+"&outFormat=json&thumbMaps=false"; | |
const init = { | |
headers: { | |
"content-type": "application/json;charset=UTF-8", | |
}, | |
} | |
const response = await fetch(endpoint,init) | |
const mqContent = await response.json() | |
// If this is a get response, just use the values from mapquest | |
city = mqContent.results[0].locations[0].adminArea5; | |
state = mqContent.results[0].locations[0].adminArea3; | |
zip = mqContent.results[0].locations[0].postalCode; | |
}else{ | |
// If this isn't a get response, just use the values from request.cf | |
city = request.cf.city; | |
state = request.cf.region; | |
zip = request.cf.postalCode; | |
latitude = request.cf.latitude; | |
longitude = request.cf.longitude; | |
} | |
// How confident am I that this location is accurate? | |
let precisionConfidence = 'neutral'; | |
if(request.cf.asOrganization == 'Spectrum'){ | |
precisionConfidence = 'high'; | |
} | |
if(request.cf.asOrganization == 'T-Mobile USA'){ | |
precisionConfidence = 'low'; | |
} | |
if(request.cf.asOrganization == 'CenturyLink'){ | |
precisionConfidence = 'low'; | |
} | |
if(request.cf.asOrganization == 'AT&T U-verse'){ | |
precisionConfidence = 'high'; | |
} | |
if(request.cf.asOrganization == 'Google Fi'){ | |
precisionConfidence = 'low'; | |
} | |
if(request.cf.asOrganization == 'Verizon Wireless'){ | |
precisionConfidence = 'low'; | |
} | |
if(request.cf.asOrganization == 'TDS Telecom'){ | |
precisionConfidence = 'high'; | |
} | |
if(request.cf.asOrganization == 'University of Wisconsin - Milwaukee'){ | |
precisionConfidence = 'high'; | |
} | |
if(request.cf.asOrganization == 'Apple'){ | |
precisionConfidence = 'high'; | |
} | |
// The struct that holds the details of who the user is | |
let user = { | |
latitude:latitude, | |
longitude:longitude, | |
city:city, | |
state:state, | |
zip:zip, | |
precisionConfidence:precisionConfidence | |
} | |
return new Response(JSON.stringify(user, 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