Skip to content

Instantly share code, notes, and snippets.

@seb-thomas
Created April 26, 2018 16:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seb-thomas/6e8de1e8415b5f69ab6f52e75f2b4e7d to your computer and use it in GitHub Desktop.
Save seb-thomas/6e8de1e8415b5f69ab6f52e75f2b4e7d to your computer and use it in GitHub Desktop.
(function () {
var submit = document.getElementById('submit')
var input = document.getElementById('postcodeInput')
var results = document.getElementById('results')
var stores = [
{
name: 'The Good Wine Shop',
lat: 51.4934165703256,
lng: -0.249506824970951
},
{
name: 'Albion Wine Shippers',
lat: 51.522069316047,
lng: -0.118444991313869
},
{
name: 'New Zealand Cellar',
lat: 51.4633202470804,
lng: -0.111691966601662
},
{
name: 'Passione Vino',
lat: 51.5248490292883,
lng: -0.0829685056508683
}
]
var storeGeojson = [
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [-0.249506824970951, 51.493416570325]
},
"properties": {
"name": "The Good Wine Shop"
}
},
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [-0.118444991313869, 51.522069316047]
},
"properties": {
"name": "Albion Wine Shippers"
}
},
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [-0.111691966601662, 51.4633202470804]
},
"properties": {
"name": "New Zealand Cellar"
}
},
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [-0.0829685056508683, 51.5248490292883]
},
"properties": {
"name": "Passione Vino"
}
}
]
function arePointsNear(checkPoint, centerPoint, km) {
var ky = 40000 / 360
var kx = Math.cos(Math.PI * centerPoint.lat / 180.0) * ky
var dx = Math.abs(centerPoint.lng - checkPoint.lng) * kx
var dy = Math.abs(centerPoint.lat - checkPoint.lat) * ky
return Math.sqrt(dx * dx + dy * dy) <= km
}
function arePointsWithinKm(from, to, km) {
return turf.distance(from, to) <= km
}
submit.addEventListener('click', function (e) {
var request = new XMLHttpRequest()
var url = 'https://api.postcodes.io/postcodes/' + encodeURI(input.value)
request.open('GET', url, true)
request.onload = function() {
if (this.status >= 200 && this.status < 400) {
// Success!
var data = JSON.parse(this.response)
// var centerPoint = { lat: data.result.latitude, lng: data.result.longitude }
var userPoint = turf.point([data.result.longitude, data.result.latitude]);
// var filteredStores = stores.filter(store => arePointsNear(store, centerPoint, 10))
var filteredPoints = storeGeojson.filter(storePoint => arePointsWithinKm(storePoint, userPoint, 5))
if (filteredPoints.length <= 0) {
results.innerHTML = 'No stores near you yet, but check back soon.'
return
}
var nearestStore = turf.nearestPoint(userPoint, turf.featureCollection(filteredPoints));
results.innerHTML = JSON.stringify(nearestStore)
} else {
// We reached our target server, but it returned an error
var data = JSON.parse(this.response)
results.innerHTML = data.error
}
console.log(this.response)
}
request.onerror = function() {
results.innerHTML = 'Connection error'
}
request.send()
})
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment