Skip to content
This repository has been archived by the owner on Jul 26, 2018. It is now read-only.

Added a button to relocate using the navigator's geolocation service. #145

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions js/geoloc.js
Original file line number Diff line number Diff line change
@@ -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);
}
3 changes: 3 additions & 0 deletions locations.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
<body>
<section class="container">
<h1>DEV FRIENDLY PLACES</h1>
<button id="geoloc" style="display:none" onclick="initGeolocation()">Locate me</button>

<p>Pick a city and find a place for coding</p>

<div class="github-fork-ribbon-wrapper right">
Expand All @@ -23,6 +25,7 @@ <h1>DEV FRIENDLY PLACES</h1>
<script type="text/javascript">
buildLocations();
</script>
<script src="/js/geoloc.js"></script>
</section>
</body>
</html>