Last active
December 30, 2015 12:09
-
-
Save yoamomonstruos/7827018 to your computer and use it in GitHub Desktop.
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
| var studios = { | |
| nyc: { | |
| coords: { | |
| latitude: 40.748224, | |
| longitude: -73.995730 | |
| } | |
| }, | |
| london: { | |
| coords: { | |
| latitude: 51.523910, | |
| longitude: -0.076880 | |
| } | |
| }, | |
| malmo: { | |
| coords: { | |
| latitude: 55.600824, | |
| longitude: 13.001198 | |
| } | |
| } | |
| }; | |
| var orderByDistance = {}; | |
| var getLocation = function() { | |
| var currentPosition; | |
| if (navigator.geolocation) { | |
| navigator.geolocation.getCurrentPosition(function(location) { | |
| // Set Current Position | |
| currentPosition = location; | |
| for (var key in studios) { | |
| var studio = { | |
| name: key, | |
| distance: calculateDistance(currentPosition, studios[key]) | |
| }; | |
| studios[key].distance = studio.distance; | |
| if (orderByDistance.hasOwnProperty("distance") === false || orderByDistance.distance > studio.distance) { | |
| orderByDistance = studio; | |
| } | |
| } | |
| console.log(studios); | |
| // console.log(firstStudio); | |
| // Arrange Footer | |
| $('.footer__contact__item[data-studio="' + orderByDistance.name + '"]').insertBefore($('.footer__contact__item[data-studio]:first')); | |
| }); | |
| } | |
| else { | |
| console.log("Default"); | |
| } | |
| } | |
| var calculateDistance = function(posA, posB) { | |
| var radius = 6371, | |
| latitudeA = posA.coords.latitude, | |
| latitudeB = posB.coords.latitude, | |
| longitudeA = posA.coords.longitude, | |
| longitudeB = posB.coords.longitude; | |
| var dLat = toRadians(latitudeB - latitudeA); | |
| var dLon = toRadians(longitudeB - longitudeB); | |
| var lat1 = toRadians(latitudeA); | |
| var lat2 = toRadians(latitudeB); | |
| var a = Math.sin(dLat/2) * Math.sin(dLat/2) + Math.sin(dLon/2) * Math.sin(dLon/2) * Math.cos(lat1) * Math.cos(lat2); | |
| var angularDistance = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); | |
| var d = radius * angularDistance; | |
| return d; | |
| } | |
| var toRadians = function(value) { | |
| return value * (Math.PI / 180) | |
| } | |
| $(document).ready(function() { | |
| getLocation(); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment