How to convert latitude or longitude to meters?
Solution 1
Here is a javascript function:
function measure(lat1, lon1, lat2, lon2){ // generally used geo measurement function
var R = 6378.137; // Radius of earth in KM
var dLat = lat2 * Math.PI / 180 - lat1 * Math.PI / 180;
var dLon = lon2 * Math.PI / 180 - lon1 * Math.PI / 180;
var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(lat1 * Math.PI / 180) * Math.cos(lat2 * Math.PI / 180) *
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 * 1000; // meters
}
Explanation: https://en.wikipedia.org/wiki/Haversine_formula
The haversine formula determines the great-circle distance between two points on a sphere given their longitudes and latitudes.
Solution 2
Given you're looking for a simple formula, this is probably the simplest way to do it, assuming that the Earth is a sphere with a circumference of 40075 km.
Length in meters of 1° of latitude = always 111.32 km
Length in meters of 1° of longitude = 40075 km * cos( latitude ) / 360
Solution 3
For approximating short distances between two coordinates I used formulas from http://en.wikipedia.org/wiki/Lat-lon:
m_per_deg_lat = 111132.954 - 559.822 * cos( 2 * latMid ) + 1.175 * cos( 4 * latMid);
m_per_deg_lon = 111132.954 * cos ( latMid );
.
In the code below I've left the raw numbers to show their relation to the formula from wikipedia.
double latMid, m_per_deg_lat, m_per_deg_lon, deltaLat, deltaLon,dist_m;
latMid = (Lat1+Lat2 )/2.0; // or just use Lat1 for slightly less accurate estimate
m_per_deg_lat = 111132.954 - 559.822 * cos( 2.0 * latMid ) + 1.175 * cos( 4.0 * latMid);
m_per_deg_lon = (3.14159265359/180 ) * 6367449 * cos ( latMid );
deltaLat = fabs(Lat1 - Lat2);
deltaLon = fabs(Lon1 - Lon2);
dist_m = sqrt ( pow( deltaLat * m_per_deg_lat,2) + pow( deltaLon * m_per_deg_lon , 2) );
The wikipedia entry states that the distance calcs are within 0.6m for 100km longitudinally and 1cm for 100km latitudinally but I have not verified this as anywhere near that accuracy is fine for my use.
Solution 4
Here is the R version of b-h-'s function, just in case:
measure <- function(lon1,lat1,lon2,lat2) {
R <- 6378.137 # radius of earth in Km
dLat <- (lat2-lat1)*pi/180
dLon <- (lon2-lon1)*pi/180
a <- sin((dLat/2))^2 + cos(lat1*pi/180)*cos(lat2*pi/180)*(sin(dLon/2))^2
c <- 2 * atan2(sqrt(a), sqrt(1-a))
d <- R * c
return (d * 1000) # distance in meters
}
Solution 5
The earth is an annoyingly irregular surface, so there is no simple formula to do this exactly. You have to live with an approximate model of the earth, and project your coordinates onto it. The model I typically see used for this is WGS 84. This is what GPS devices usually use to solve the exact same problem.
NOAA has some software you can download to help with this on their website.
Related videos on Youtube
Adam Taylor
Developer with interests across marketing, entrepreneurship and technology. Strong professional experience with Perl but equally happy with other dynamic open source languages. Keen cyclist.
Updated on June 29, 2021Comments
-
Adam Taylor almost 3 years
If I have a latitude or longitude reading in standard NMEA format is there an easy way / formula to convert that reading to meters, which I can then implement in Java (J9)?
Edit: Ok seems what I want to do is not possible easily, however what I really want to do is:
Say I have a lat and long of a way point and a lat and long of a user is there an easy way to compare them to decide when to tell the user they are within a reasonably close distance of the way point? I realise reasonable is subject but is this easily do-able or still overly maths-y?
-
John D. Cook about 15 yearsYou may find this document helpful: http://www.johndcook.com/lat_long_details.html
-
Midas Kwant about 15 yearsDo you mean to UTM? en.wikipedia.org/wiki/…
-
Baltimark about 15 yearsWhat do you mean by converting a lat/long to meters? meters from where? Are you looking for a way to compute the distance along the surface of the earth from one coordinate to another?
-
Midas Kwant about 15 yearsThese seems like a copy of this question stackoverflow.com/questions/176137/java-convert-lat-lon-to-utm
-
Adam Taylor about 15 yearsOk I think I maybe asked the wrong question.. I've updated it.
-
Baltimark about 15 yearsDefine "waypoint". Define "reasonable". Is this really what you want to know: "how do you calculate the distance between two points given their latitude and longitude?"
-
Kristof Van Landschoot about 12 yearsI stumbled upon this question wanting to do SQL queries on latitude and longitude and found this great article with some Java code at the bottom. It might interest you as well.
-
jb. over 10 yearspossible duplicate of Simple calculations for working with lat/lon + km distance?
-
Teepeemm about 9 yearspossible duplicate of How do I calculate distance between two latitude-longitude points?
-
PM 2Ring over 5 yearsMost of the answers here are using simple spherical trigonometry, so the results are rather crude compared to the WGS84 ellipsoid distances used in the GPS system. Some of the answers refer to Vincenty's formula for ellipsoids, but that algorithm was designed for use on 1960s' era desk calculators and it has stability & accuracy issues; we have better hardware and software now. Please see GeographicLib for a high quality library with implementations in various languages. (I'm not sure what GDAL uses for its WGS84 work, I think it still uses Vincenty).
-
-
RickyA over 11 yearsNo, that does not work! The x distance in m is different for different values of latitude. At the equator you might get away with it, but the closer you get to the poles the extremer your ellipsoids will get.
-
tshepang about 11 yearsI see the link is full of broken.
-
High Performance Mark over 10 yearsNo, the nautical mile is defined by international standard (v en.wikipedia.org/wiki/Nautical_mile) to be 1852m. Its relationship to the measurement of an arc on the surface of a spheroid such as the Earth is now both historical and approximate.
-
JivanAmara over 10 yearsWhile your comment is reasonable, it doesn't answer the user's question about converting the lat/lng degree difference to meters.
-
Ben Hutchison over 10 yearsfinally, a straightforward answer :)
-
OMGPOP about 10 yearswhat if latitude is -179 and the other is 179, the x distance should be 2 degrees instead of 358
-
Aram Kocharyan over 9 yearsFor those looking for a library to convert between wgs and utm: github.com/urbanetic/utm-converter
-
CPBL about 9 yearsDo not use this answer (for some reason, it's upvoted). There is not a single scaling between longitude and distance; the Earth is not flat.
-
Abel Callejo over 8 yearsI believe it is 111.1
-
Ben over 7 yearsNote that one degree of longitude is 111 km at the equator, but less for other latitudes. There's a simple approximative formula to find the length in km of 1° of longitude in function of latitude : 1° of longitude = 40000 km * cos (latitude) / 360 (and of course it gives 111 km for latitude = 90°). Also remark that 1° of longitude is almost always a different distance than 1° of latitude.
-
Ravindranath Akila over 7 yearsWould be really grateful if somebody could add in some explanatory comments on the above code. Thanks in advance!
-
Gorka Llona about 7 yearsNote that in 2017 the Wikipedia page has another (seems refined) formula.
-
Joachim about 7 yearsFound this which this comment seem to be an adoption of. The link also says its based on this article on distance calculation. So any unanswered questions should be found in the original link. :)
-
Reece almost 7 yearsHow does the longitude equation work? with a latitude of 90 degrees you'd expect it to show near 111km; but instead it shows 0; similarly, values close to it are also near 0.
-
Ben almost 7 yearsLatitude is 0° at the equator and 90° at the pole (and not the opposite). For equator the formula gives 40075 km * cos(0°) / 360 = 111 km. For pole the formula gives 40075 * cos(90°) / 360 = 0 km.
-
not2qubit over 6 yearsYes, the formula in Wikipedia is slightly different, but it seem that the other Wikipedia formula is based on the similar results from this great SO answer, where someone actually went through the calculations.
-
derHugo about 4 years
it is assuming the earth is a circle
^^ Some strange people do this nowadays ... but what you mean is probably ratherit is assuming the earth is a sphere
;) -
Ben about 4 yearsI think this approach is simple especially as the question didn't ask for the exact distance between two points, but rather if they are "reasonably close enough".With these formulas we easily check if the user is within a square centered on the waypoint. It's much simpler to check for a square than for a circle.
-
dangalg over 3 yearsHow do I add elevation into this calculation?
-
Marco Aurélio da Silva over 3 years@dangalg, assuming lower distances where the floor is plane, you have also altitudes
alt1
andalt2
, anddm
is the distance in meters (the result ofmeasure
function above). You can use the hypothenuse function of JSMath.hypot(x, y)
, wherex
isdm
andy
ismax(alt1, alt2) - min(alt1, alt2)
. -
Eli Holmes over 3 yearsKeep in mind that in this equation "latMid" is in radians while "m_per_deg_lat" is for degrees. So if you want to compute this for a latitude of 30N (say), in the equation
latMid = pi*30/180
. -
EngrStudent over 3 yearsI think you have a typo for this: m_per_deg_lon because inputs might need to be lon and not lat.
-
André about 2 years@EngrStudent No, he is right, the transformation factor for longitude depends on the latitude, since the distance between meridians gets smaller until they meet at the poles, so the m_per_long_degree gets smaller too