Issue
$http.get('*****').success(function(data,dealers,response)
{
$scope.items = data;
$scope.item ='';
console.log(data);
var shoplatit=data.S_Location.Latitude;
var shoplong=data.S_Location.Longitude;
});
$scope.nearme = function($scope) {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function (position) {
mysrclat = position.coords.latitude;
mysrclong = position.coords.longitude;
console.log(mysrclat);
console.log(mysrclong);
});
}
i tried like this but its not working getting deg2rad is not defined erro
var lat1 =15.008809;
var lat2 =78.659096;
var lon1 =13.90457539;
var lon2 =78.5855514
var R = 6371; // Radius of the earth in km
var dLat = deg2rad(lat2-lat1); // deg2rad below
var dLon = deg2rad(lon2-lon1);
var a =
Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) *
Math.sin(dLon/2) * Math.sin(dLon/2)
;
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
var d = R * c; // Distance in km
console.log(d);
<div ng-controller="ListingCtrl" >
<div data-ng-init="nearme()">
<div class="list card" data-ng-repeat="item in items">
<h2>{{item.Store_Name}} </h2>{{item.S_Location.Latitude}}<br>{{item.S_Location.Longitude}}
<p>{{item.S_Address}}</p>
<h1>here i want show distance in km</h1>
</div>
</div>
</div>
I have stored store latitude and longitude in mongodb. In listing page i am getting customer latitude and langitude using navigator.geolocation.i want to calculate distance between two latitiude and longitude .i have enclosed my code
Solution
This lat-long debug may help you to debug geographical distance and other aspects easily.
Below is the functional JS code to calculate it; also it has deg2rad()
function, which is used for converting the number in degrees to the radian equivalent (which appears to be missing in your code).
I have used two coordinates provided by you, and the distance returned is 123.04 km
, also crosschecked through Google Maps to verify same.
/*
Title: Get Distance from two Latitude / Longitude in Kilometers.
Description: This Javascript snippet calculates great-circle distances between the two points
—— that is, the shortest distance over the earth’s surface; using the ‘Haversine’ formula.
*/
function _getDistanceFromLatLonInKm(lat1, lon1, lat2, lon2) {
var R = 6371; // Radius of the earth in kilometers
var dLat = deg2rad(lat2 - lat1); // deg2rad below
var dLon = deg2rad(lon2 - lon1);
var a =
Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) *
Math.sin(dLon / 2) * Math.sin(dLon / 2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
var d = R * c; // Distance in KM
return d;
}
function deg2rad(deg) {
return deg * (Math.PI / 180)
}
// Point 1: 15.008809, 78.659096
// Point 2: 13.90457539, 78.5855514
var _lat1 = 15.008809;
var _lon1 = 78.659096;
var _lat2 = 13.90457539;
var _lon2 = 78.5855514;
// precise value
var _d = "Precise value: " + _getDistanceFromLatLonInKm(_lat1, _lon1, _lat2, _lon2);
console.log(_d); // alert(_d);
// round value
_d = "Round value: " +
Math.round(_getDistanceFromLatLonInKm(_lat1, _lon1, _lat2, _lon2) * 100) / 100 +
" km";
console.log(_d); // alert(_d);
Answered By - Mayura
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.