Created
November 26, 2017 19:23
-
-
Save gustavo-depaula/f0f0a76da816888c958cd397dd71d590 to your computer and use it in GitHub Desktop.
This file contains 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
bathroomsByDistance (userLocation) { | |
let dist = (a, b) => { | |
lat1 = a.lat | |
lon1 = a.lon | |
lat2 = b.lat | |
lon2 = b.lon | |
function rad(radiano) { | |
return radiano*Math.PI/180; | |
} | |
var R = 6378.137; | |
var dLat = rad( lat2 - lat1 ); | |
var dLong = rad( lon2 - lon1 ); | |
var a = Math.sin(dLat/2) * Math.sin(dLat/2) + Math.cos(rad(lat1)) * Math.cos(rad(lat2)) * Math.sin(dLong/2) * Math.sin(dLong/2); | |
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); | |
var d = R * c; | |
return d.toFixed(6); | |
} | |
let compareDist = (b1, b2) => { | |
if (dist(userLocation, b1) < dist(userLocation, b2)) | |
return -1; | |
if (dist(userLocation, b1) > dist(userLocation, b2)) | |
return 1; | |
return 0; | |
} | |
return this.banheiros.sort(compareDist) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment