I Want Compare my Current latitude & longitude to another latitude & longitude and found near location to me
how can i calculate this ?
my location :
51.510836, -0.127579
another :
51.547385, 0.020022
51.482876, -0.036542
51.511894, -0.248477
As i understand
you problem is to get closest location via lat long
And you want to calculate which one is closest from your location
if yes then one by one compare all these 3 lat, long with your location's lat long.
The nearest location is which one has lowest distance in km.
try this.
function getDistanceFromLatLonInKm(lat1,lon1,lat2,lon2) {
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
return d;
}
function deg2rad(deg) {
return deg * (Math.PI/180)
}
where lat1, lon1 is your location and lat2,lon2 are those points you wanna compare simply apply it in loop and pass compared locations and finally a distance which is lowest is your answer.
Related
I need to calculate the distance between two latitude and longitude points. I found this javascript code which I suppose I want.
Here comes the problem. I add the two positions lat and lng values in, and sometimes it just gives random output. What happens is two points literally next to other are sometimes like 8000 meters away, but two other much furthest points return only 1500 meters for example.
function degreesToRadians(degrees) {
return degrees * Math.PI / 180;
}
function getDistanceFromLatLonInKm(lat1,lon1,lat2,lon2) {
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
return d;
}
function deg2rad(deg) {
return deg * (Math.PI/180)
}
const distanceR = getDistanceFromLatLonInKm(userLat, userLng, solLat, solLng)
Lat: 68.00757101804007, lng: -49.306640625
to
lat: 73.26312194058698 lng: -23.535461425781254
is 1143 kilometres, but these two points are next to each other.
lat: 66.75724984139227, lng: -16.259765625000004
to
lat: 71.99597405683693 lng:-42.31933593750001
is 1161 metres and the points are much farther then the previus one.
Here I think it calculates fine, unlike the two previous lat and lng points.
I've tested the examples you provided and did a few on my own and I believe your implementation is working fine. I did however get different results using your examples and code.
The first example returns approx. 1102km, which seems close to the distance using a visualizer.
The second example returns 1161 kilometres which visualized again seems about right.
Please note: the images in the links were constructed using gpsvisualizer.com which uses a Vincenty formula to calculate distance, hence the slight variation in distance numbers.
function getDistanceFromLatLonInKm(lat1, lon1, lat2, lon2) {
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
return d;
}
function deg2rad(deg) {
return deg * (Math.PI / 180)
}
const e1 = {
lat1: 68.00757101804007,
lon1: -49.306640625,
lat2: 73.26312194058698,
lon2: -23.535461425781254
}
const e2 = {
lat1: 66.75724984139227,
lon1: -16.259765625000004,
lat2: 71.99597405683693,
lon2: -42.31933593750001
}
const dist1 = getDistanceFromLatLonInKm(e1.lat1, e1.lon1, e1.lat2, e1.lon2);
const dist2 = getDistanceFromLatLonInKm(e2.lat1, e2.lon1, e2.lat2, e2.lon2);
console.log(`Example 1 distance: ${dist1}km. Example 2 distance: ${dist2}km`);
I can not reproduce the issue you are having so I believe your error lies in your visualization of coordinates.
Is there a way to calculate the travelled distance in Mapbox?
For example: if a user clicks the start button it'll record the travelled path and calculate the distance. I know it's possible to calculate a distance between two geopoints described here, however a user can go off-road and therefore won't choose to walk the fastest/most suited route between two geopoints. I would like to sum ALL walked kilometers.
Monitor position
navigator.geolocation.watchPosition(function(position) {
document.getElementById('distance').innerHTML =
calculateDistance(startPos.coords.latitude, startPos.coords.longitude,
position.coords.latitude, position.coords.longitude);
});
Calculate distance
function calculateDistance(lat1, lon1, lat2, lon2) {
var R = 6371; // km
var dLat = (lat2 - lat1).toRad();
var dLon = (lon2 - lon1).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));
var d = R * c;
return d;
}
Number.prototype.toRad = function() {
return this * Math.PI / 180;
}
EDIT: one possible solution could be that every time I get a new location I calculate distance between previous geopoint and new geopoint and use that to calculate distance. If I want to see total distance I sum all the distances between previous points. I can also use that data to plot a line between all stored geopoints. Does this makes sense? Is there a more efficient way to do this since this is a pure javascript solution (maybe there is a more related to Mapbox solution?)
I have a JSON object of latitude and longitude locations. I want to list them in the order they are closest to my current lat and long. Is there a way to query using AJAX to get an array of locations in the order of their proximity to my current location. If not, what the best approach I can use.
You can enumerate yours array of coordinates with this function (from this answer):
function getDistanceFromLatLonInKm(lat1,lon1,lat2,lon2) {
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
return d;
}
function deg2rad(deg) {
return deg * (Math.PI/180)
}
Or you can divide yours coordinates array per sector like:
if lat1 is greater than 70.000 and lesser than 75.000 then this is sector A and so on.
Finally, when you have these sectors you can search in specific one to avoid long calculations.
Let's say that i have a database of 100,000 coordinates,
i need to find which ones are less than 1 mile away from a certain coordinate.
What's the most efficient way to do this?(in any language)
Firstly, we have to write a basic function to calculate the distance between 2 points:
function distance(lat1, lon1, lat2, lon2) {}
For this example, I will base the function on the formula of a spherical earth projected to a plane (here)
Firstly, we have to calculate the deltas (the distance in degrees between the lat and longs) and the mean latitude (average of latitudes):
var dLat = lat1 - lat2;
var dLon = lon1 - lon2;
var mLat = (lat1 + lat2) / 2;
var earthRadius = 3959; //in miles
Then we convert those to Radians using d=180/PI rad:
dLat = dLat * 180 / 3.1415926535;
dLon = dLon * 180 / 3.1415926535;
mLat = mLat * 180 / 3.1415926535;
Now, we use the formula to transform our data into distance:
var distance = earthRadius * (dLat * dLat + Math.pow(Math.cos(mLat) * dLon, 2));
And return the distance
return distance;
Now, just iterating through all the points and checking if the distance is ok for each one. Let's say a point is described in this way:
var p = {
lat = ...
lon = ...
}
And assuming there is a list of points (for example, named points) and a reference point (for example, named ref).
var result = []
points.forEach(function (d) {
if (distance(d.lat, d.lon, ref.lat, ref.lon) <= 1) {
result.push(d);
}
};
You can also check for latitude boundary box - longtitude requires more complex calculations and it's just a waste of time. You can determine a mile in degrees is 1/69 deg/mile (approximately 0.1449 degrees). So you can check which points are outside of this boundary box:
var result = []
var maxLat = ref.lat + 0.1449;
var minLat = ref.lat - 0.1449;
points.forEach(function (d) {
if (d.lat > maxLat || d.lat < minLat) continue;
if (distance(d.lat, d.lon, ref.lat, ref.lon) <= 1) {
result.push(d);
}
};
Then you should finish with an array of points that are closer than 1 mile from the reference point.
I might have a mistake in the formula things (I'm more like a programmer than a mathematican). So double check if they work with the Wikipedia article I added a link to.
I'm developing a node.js web-app where I'm using mongoose and MongoDB for storage. It's a gelocation app. so sometimes I have to find the 10 nearest places stored in database. I have this method to calculate this distance:
var haversine = function(p1, p2) {
var R = 6371; // km
var dLat = toRad(p2.lat-p1.lat);
var dLon = toRad(p2.lon-p1.lon);
var lat1 = toRad(p1.lat);
var lat2 = toRad(p2.lat);
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 c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
var d = R * c;
return d;
}
where p1 and p2 are points {lat: Integer, lon: Integer}.
So, How should I do to find the nearest places?
Apologies if I am not understanding the question, but if you have a lat and lon stored in MongoDB already, why not just ensure a Geo index on that key and use MongoDB itself to find the nearest points.
Assuming a Geo index on location key consisting of lat and lon, you can simply issue something similar to the following:
db.places.find( { location : { $near : [lon,lat] } } ).limit(10)
Which should find the nearest ten places in your database to the location defined at lon,lat.
You can find more information at the documentation for geospatial indexing.