I am currently developing an application that will organize locations based on either name or distance from the user. Everything is functioning properly except for retrieving the distance. I believe I should be able to obtain the user's coordinates through geolocation and already have the coordinates for each location. Is it feasible to calculate the distance using the haversine formula with these coordinates and then assign the distance to each location using object.distance = d
? Below is my code and a link to the project Plunk.
Plunker: http://plnkr.co/edit/nRQc7Ym0lsaK6jQwd626?p=preview
Code:
var app = angular.module('app', []);
app.controller('firstCtrl', function($scope) {
$scope.ASiteLocs = [{
"name": "IL5077 BRUSSELS",
"styleUrl": "#waypoint",
"Point": {
"coordinates": "-90.58543899999999,38.955472,0"
}
}, {
"name": "IL5076 KAMPSVILLE",
"styleUrl": "#waypoint",
"Point": {
"coordinates": "-90.661923,39.29403,0"
}
}, {
"name": "IL5146 CARROLLTON",
"styleUrl": "#waypoint",
"Point": {
"coordinates": "-90.39965700000001,39.309142,0"
}
}, {
"name": "IL5153 GREENFIELD",
"styleUrl": "#waypoint",
"Point": {
"coordinates": "-90.208747,39.364077,0"
}
}];
$scope.SSiteLocs = [More Locations...];
$scope.SiteLocs = $scope.SSiteLocs.concat($scope.ASiteLocs);
repoSortOrder = "site.name";
navigator.geolocation.getCurrentPosition(GetLocation);
function GetLocation(location) {
Lat = location.coords.latitude;
Lon = location.coords.longitude;
}
angular.forEach($scope.SSiteLocs, function(object) {
object.carrier = 'Sprint';
getCoordDistance();
object.distance = $scope.d
});
angular.forEach($scope.ASiteLocs, function(object) {
object.carrier = 'AT&T';
getCoordDistance();
object.distance = $scope.d
});
angular.forEach($scope.SiteLocs, function(location) {
var clength = location.Point.coordinates.length;
if (location.Point.coordinates.substring(clength - 2, clength) === ",0") {
location.Point.coordinates = location.Point.coordinates.substring(0, clength - 2).split(",");
Lat = location.Point.coordinates[0];
Lon = location.Point.coordinates[1];
Com = ",";
location.Point.coordinates = Lon.concat(Com, Lat);
}
});
function getCoordDistance() {
Number.prototype.toRad = function() {
return this * Math.PI / 180;
}
var lat2 = Lat;
var lon2 = Lon;
var lat1 = 45;//Test Lat
var lon1 = -50;//Test Lon
var R = 3959; // Radius in miles
//has a problem with the .toRad() method below.
var x1 = lat2 - lat1;
var dLat = x1.toRad();
var x2 = lon2 - lon1;
var dLon = x2.toRad();
var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.cos(lat1.toRad()) * Math.cos(lat2.toRad()) *
Math.sin(dLon / 2) * Math.sin(dLon / 2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
$scope.d = R * c;
}
});
In the provided code, when using integers for the Lat/Lons in getCoordDistance()
(e.g. lat1 = 5
,lat2 = 10
,lon1 = 0
,lon2 = 0
), it successfully calculates and adds the distance to each location. However, when attempting to use my actual location, it fails. Any suggestions?
Thank you in advance for any assistance.