Calculate Distance in Km and Miles - distance

I have two points whose latitude and longitude i know.
How can i calculate the distance(in Km and Miles) between them. What is the formulae?

You can use the haversine formula to calculate such distances.

Use the haversine Formula for this...
Here is the link having java script code to calculate distance
http://www.movable-type.co.uk/scripts/latlong.html

A = LAT1, B = LONG1
C = LAT2, D = LONG2 (all converted to radians: degree/57.29577951)
IF A = C AND B = D THEN DISTANCE = 0;
ELSE
IF [SIN(A)SIN(C)+COS(A)COS(C)COS(B-D)] > 1 THEN DISTANCE = 3963.1*ARCOS[1];
ELSE
DISTANCE=3963.1*ARCOS[SIN(A)SIN(C)+COS(A)COS(C)COS(B-D)];

For an accurate and complete (works with any pair of points) solution
use my geodesic calculator at
http://geographiclib.sf.net/cgi-bin/GeodSolve. The formulas are given in
http://arxiv.org/abs/1102.1215.

Related

Compute coordinates position with projection

Given 2 coordinates (point 1 and 2 in red) in WGS84 I need to find the coordinates of the point perpendicular (point 3) to the line at a given distance.
I could manage to make the math to compute this perpendicular point, but when displayed on the map, the point seems to be at a wrong place, probably because of the projection.
What I want on a map:
And what I have instead on the map:
How can I take into account the projection so that the point on the map appears perpendicular to the line? The algorithm below to compute the point comes from here: https://math.stackexchange.com/questions/93424/calculate-rectangle-coordinates-from-line-and-height
public static Coords ComputePerpendicularPoint(Coords first, Coords last, double distance)
{
double slope = -(last.Lon.Value - first.Lon.Value) / (last.Lat.Value - first.Lat.Value);
// number of km per degree = ~111km (111.32 in google maps, but range varies between 110.567km at the equator and 111.699km at the poles)
// 1km in degree = 1 / 111.32km = 0.0089
// 1m in degree = 0.0089 / 1000 = 0.0000089
distance = distance * 0.0000089 / 100; //0.0000089 => represents around 1m in wgs84. /100 because distance is in cm
double t = distance / Math.Sqrt(1 + (slope * slope));
Coords perp_coord = new Coords();
perp_coord.Lon = first.Lon + t;
perp_coord.Lat = first.Lat + (t * slope);
return perp_coord;
}
Thank you in advance!

Matlab GPS + distance in meters

I have to calculate for a given Latitude (lat0) and Longitude (lon0) the new latitude (lat1) and longitude (lon1) if I move a distance x[m] and y[m] away from the initial position. For example:
clear all; clc;
%initial coordinates:
lat0=56;
lon0=5;
%moving away from lat0,lon0,
xcor=200; %[m]
ycor=100; %[m]
First I use this code to calculate lat1 and lon1: (source: Adding distance to a GPS coordinate)
lat1=lat0+rad2deg((xcor/6372800))
lon1=lon0+rad2deg((ycor/6372800)/(cos(lat0)))
distance=sqrt(xcor^2+ycor^2)
Now i want to check my answer with the haversine equation:
dlat = deg2rad(lat1-lat0);
dlon = deg2rad(lon1-lon0);
lat0 = deg2rad(lat0);
lat1 = deg2rad(lat1);
a = (sin(dlat./2)).^2 + cos(lat0) .* cos(lat1) .* (sin(dlon./2)).^2;
c = 2 .* asin(sqrt(a));
distance_check=6372800*c
However, most of the time the distance from haversine is different with 100+ meters compared to the calculated distance in the first 3 lines of code.
What is going wrong in this code?
In the first code, 2nd line, cos(lat0) has to be cos(deg2rad(lat0)).

Calculate Latitude and longitude more between Latitude/Longitude points?

Latitude: 22.744812,
Longitude: 75.892578
The above would be considered my center point.
And now I need to determine the latitude and longitude points from center point 1000 meter outward to each NSWE corners. So I would have a central long/lat, N, S, E and W long/lat..
So I would end up with 4 additional lat/long pairs.
What I am trying to resolve is a formula, preferably that can be done on a standard calculator to determine these 4 NSWE points based on the central point.
You could use MapKit for that:
- (CLLocationCoordinate2D *) calculateSquareCoordinates:(CLLocation*)center withRadius:(float)radius{
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(center.coordinate, radius*2, radius*2);
CLLocationCoordinate2D points[4];
points[0] = CLLocationCoordinate2DMake(region.center.latitude - region.span.latitudeDelta/2, region.center.longitude - region.span.longitudeDelta/2);
points[1] = CLLocationCoordinate2DMake(region.center.latitude + region.span.latitudeDelta/2, region.center.longitude - region.span.longitudeDelta/2);
points[2] = CLLocationCoordinate2DMake(region.center.latitude + region.span.latitudeDelta/2, region.center.longitude + region.span.longitudeDelta/2);
points[3] = CLLocationCoordinate2DMake(region.center.latitude - region.span.latitudeDelta/2, region.center.longitude + region.span.longitudeDelta/2);
return points;
}
and just call
CLLocationCoordinate2D *fourPoints = [self calculateSquareCoordinates:center withRadius:1000];
on your code.
you will have to use the Haversine formula to calculate the Lat/Long based on distance from a starting Lat/Long. have a look at this Link
The average radius of the earth is around 6371000 metres. This means that
1 degree of lattitude is equivalent to 6371000 * PI / 180 metres
(NB: PI = 3.14159... etc). However, 1 degree of longitude depends on the lattitude that you are. At the equator, one degree of longitude corresponds to the same distance in metres as 1 degree of lattitude. However, at the north and south poles, all longitude values are the same point (i.e. the pole itself), so 1 degree of longitude at the poles is zero metres. The formula for longitude is
1 degree of longitude is equivalent to 637100 * PI / 180 * COS(Lattitude)
where COS is the trigonometric cosine function. If you make these conversions, then you can do the calculation on a standard calculator. However, be aware that these are approximations that work well over short distances (e.g. less than a few hundred kilometers), but over long distances (e.g. thousands of kilometers) they become more and more inaccurate.

Distance between two coordinates in php using haversine

I've looked around and seen mention of the haversine formula to determine distance between two coordinates (lat1, lng1) and (lat2, lng2).
I've implemented this code:
function haversineGreatCircleDistance(
$latitudeFrom, $longitudeFrom, $latitudeTo, $longitudeTo, $earthRadius = 6371000)
{
// convert from degrees to radians
$latFrom = deg2rad($latitudeFrom);
$lonFrom = deg2rad($longitudeFrom);
$latTo = deg2rad($latitudeTo);
$lonTo = deg2rad($longitudeTo);
$latDelta = $latTo - $latFrom;
$lonDelta = $lonTo - $lonFrom;
$angle = 2 * asin(sqrt(pow(sin($latDelta / 2), 2) +
cos($latFrom) * cos($latTo) * pow(sin($lonDelta / 2), 2)));
return $angle * $earthRadius;
}
And am trying to determine:
1) what units this is returning? (goal being in feet)
2) is this equation written the right way?
For example what should be the distance between these two points?
(32.8940695525,-96.7926336453) and (33.0642604502, -96.8064332754)?
I'm getting 18968.0903312 from the formula above.
Thanks!
1) what units this is returning? (goal being in feet)
Whatever units in which you supply the Earth's radius.
2) is this equation written the right way?
Test it. You can compare your results with an existing Haversine formula implementation, like this one.

How do I calculate the distance between two points of latitude and longitude? [duplicate]

This question already has answers here:
Distance between two coordinates with CoreLocation
(4 answers)
Closed 8 years ago.
i have latitude and longitude of particular place and i want to calculate the distance so how can i calculate it?
CLLocation *location1 = [[CLLocation alloc] initWithLatitude:lat1 longitude:long1];
CLLocation *location2 = [[CLLocation alloc] initWithLatitude:lat2 longitude:long2];
NSLog(#"Distance i meters: %f", [location1 distanceFromLocation:location2]);
[location1 release];
[location2 release];
You also need to add CoreLocation.framework to your project, and add the import statement:
#import <CoreLocation/CoreLocation.h>
This might not be the most efficient method of doing it, but it will work.
Your two locations specified by latitude and longitude can be considered vectors. Assuming that the coordinates have been converted into cartesion coordinates, calculate the dot product of the two vectors.
Given v1 = (x1, y1, z1) and v2 = (x2, y2, z2), then ...
v1 dot v2 = magnitude(v1) * magnitude(v2) * cos (theta)
Conveniently, the magnitude of v1 and v2 will be the same ... the radius of the earth (R).
x1*x2 + y1*y2 + z1*z2 = R*R*cos(theta)
Solve for theta.
theta = acos ((x1*x2 + y1*y2 + z1*z2) / (R * R));
Now you have angle between the two vectors in radians. The distance betwen the two points when travelling across the surface of earth is thus ...
distance = theta * R.
There is probably an easier way to do this entirely within the context of spherical coordinates, but my math in that area is too fuzzy--hence the conversion to cartesian coordinates.
To convert to cartesian coordinates ...
Let alpha be the latitude, and beta be the longitude.
x = R * cos (alpha) * cos (beta)
y = R * sin (alpha)
z = R * cos (alpha) * sin (beta)
Don't forget that the math function typically deal in radians, and the latitude/longitude deal in degrees.
I've cranked through the math, and can now greatly simplify the solution.
Imagine if we spin the earth so that our first vector is at 0 degrees latitude and 0 degrees longitude. The second vector would be at (alpha2 - alpha1) degrees latitude and (beta2 - beta1) degrees latitude.
Since ...
sin(0) = 0 and cos(0) = 1
our dot product simplies to ...
cos(delta_alpha) * cos(delta_beta) = cos(theta)
The rest of the math remains unchanged.
theta = acos (cos(delta_alpha) * cos(delta_beta))
distance = radius * theta
Hope this helps.