From aab61deacb567e4eda8e67eb881930875be8cfb2 Mon Sep 17 00:00:00 2001 From: vferries Date: Sun, 26 Apr 2015 03:16:12 +0200 Subject: [PATCH] Added a button to relocate using the navigator's geolocation service. --- js/geoloc.js | 25 +++++++++++++++++++++++++ locations.html | 3 +++ 2 files changed, 28 insertions(+) create mode 100644 js/geoloc.js diff --git a/js/geoloc.js b/js/geoloc.js new file mode 100644 index 0000000..562d673 --- /dev/null +++ b/js/geoloc.js @@ -0,0 +1,25 @@ +if ("geolocation" in navigator) { + document.getElementById('geoloc').style.display = "inline-block"; +} + +function initGeolocation() { + var onSuccess = function(geoPosition) { + var minDistance, nearestLocation, currentDistance, locations = getLocations(); + var lat1 = geoPosition.coords.latitude; + var lon1 = geoPosition.coords.longitude; + var squaredDistanceFromCurrent = function(location) { + return Math.pow(lat1 - location.lat, 2) + Math.pow(lon1 - location.lon, 2); + }; + for (var locationName in locations) { + currentDistance = squaredDistanceFromCurrent(locations[locationName]); + if (minDistance == undefined || currentDistance < minDistance) { + minDistance = currentDistance; + nearestLocation = locationName; + } + } + if (nearestLocation) { + window.location.href = "http://" + nearestLocation + ".devfriendlyplaces.net"; + } + }; + navigator.geolocation.getCurrentPosition(onSuccess); +} \ No newline at end of file diff --git a/locations.html b/locations.html index 3aa4e63..4888e0a 100644 --- a/locations.html +++ b/locations.html @@ -9,6 +9,8 @@

DEV FRIENDLY PLACES

+ +

Pick a city and find a place for coding

@@ -23,6 +25,7 @@

DEV FRIENDLY PLACES

+