Calculating new longitude, latitude from old + n meters

106,903

Solution 1

The number of kilometers per degree of longitude is approximately

(pi/180) * r_earth * cos(theta*pi/180)

where theta is the latitude in degrees and r_earth is approximately 6378 km.

The number of kilometers per degree of latitude is approximately the same at all locations, approx

(pi/180) * r_earth = 111 km / degree 

So you can do:

new_latitude  = latitude  + (dy / r_earth) * (180 / pi);
new_longitude = longitude + (dx / r_earth) * (180 / pi) / cos(latitude * pi/180);

As long as dx and dy are small compared to the radius of the earth and you don't get too close to the poles.

Solution 2

The accepted answer is perfectly right and works. I made some tweaks and turned into this:

double meters = 50;

// 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
double coef = meters * 0.0000089;

double new_lat = my_lat + coef;

// pi / 180 = 0.018
double new_long = my_long + coef / Math.cos(my_lat * 0.018);

Hope this helps too.

Solution 3

For latitude do:

var earth = 6378.137,  //radius of the earth in kilometer
    pi = Math.PI,
    m = (1 / ((2 * pi / 360) * earth)) / 1000;  //1 meter in degree

var new_latitude = latitude + (your_meters * m);

For longitude do:

var earth = 6378.137,  //radius of the earth in kilometer
    pi = Math.PI,
    cos = Math.cos,
    m = (1 / ((2 * pi / 360) * earth)) / 1000;  //1 meter in degree

var new_longitude = longitude + (your_meters * m) / cos(latitude * (pi / 180));

The variable your_meters can contain a positive or a negative value.

Solution 4

I had to spend about two hours to work out the solution by @nibot , I simply needed a method to create a boundary box given its center point and width/height (or radius) in kilometers:

I don't fully understand the solution mathematically/ geographically. I tweaked the solution (by trial and error) to get the four coordinates. Distances in km, given the current position and distance we shift to the new position in the four coordinates:

North:

private static Position ToNorthPosition(Position center, double northDistance)
{
    double r_earth = 6378;
    var pi = Math.PI;
    var new_latitude = center.Lat + (northDistance / r_earth) * (180 / pi);
    return new Position(new_latitude, center.Long);
}

East:

private static Position ToEastPosition(Position center, double eastDistance)
{
    double r_earth = 6378;
    var pi = Math.PI;
    var new_longitude = center.Long + (eastDistance / r_earth) * (180 / pi) / Math.Cos(center.Lat * pi / 180);
    return new Position(center.Lat, new_longitude);
}

South:

private static Position ToSouthPosition(Position center, double southDistance)
{
    double r_earth = 6378;
    var pi = Math.PI;
    var new_latitude = center.Lat - (southDistance / r_earth) * (180 / pi);
    return new Position(new_latitude, center.Long);
}

West:

private static Position ToWestPosition(Position center, double westDistance)
{
    double r_earth = 6378;
    var pi = Math.PI;
    var new_longitude = center.Long - (westDistance / r_earth) * (180 / pi) / Math.Cos(center.Lat * pi / 180);
    return new Position(center.Lat, new_longitude);
}

Solution 5

Have you checked out: How do I find the lat/long that is x km north of a given lat/long ?

These calculations are annoying at best, I've done many of them. The haversine formula will be your friend.

Some reference: http://www.movable-type.co.uk/scripts/latlong.html

Share:
106,903

Related videos on Youtube

Benjamin Udink ten Cate
Author by

Benjamin Udink ten Cate

Updated on July 08, 2022

Comments

  • Benjamin Udink ten Cate
    Benjamin Udink ten Cate almost 2 years

    I want to create 2 new longitude and 2 new latitudes based on a coordinate and a distance in meters, I want to create a nice bounding box around a certain point. It is for a part of a city and max ±1500 meters. I therefore don't think the curvature of earth has to be taken into account.

    So I have 50.0452345 (x) and 4.3242234 (y) and I want to know x + 500 meters, x - 500 meters, y - 500 meters, y + 500 meters

    I found many algorithms but almost all seem to deal with the distance between points.

  • Benjamin Udink ten Cate
    Benjamin Udink ten Cate over 12 years
    if you work for a quite small area, is it really bad to just do latitude-0.09 and longtitude-0.0148 to get approximately a square km area?
  • Ryan Ternier
    Ryan Ternier over 12 years
    I'd say it's not really bad. The square km at that level will not be distorted by the curvature of the Earth - as long as the Lat/Lng's you're dealing with is decimal.
  • nibot
    nibot over 12 years
    @BenjaminUdinktenCate That will work in Amsterdam, but will be inaccurate in other parts of the world. Doing "longitude-0.0148" will only get you about 0.16 km at the equator.
  • josch
    josch about 10 years
    to convert from degree to radians you multiply with py and divide by 180. But you write cos(latitude*180/pi)
  • Alex Essilfie
    Alex Essilfie almost 10 years
    @josch: Good catch. Try to correct the answer the answer next time instead of simply proposing a correction. Many people simply copy and paste code from StackOverflow thinking it is correct and ready to use.
  • blazs
    blazs over 7 years
    Why is latitude used in the expression for new_longitude?
  • LeoFarage
    LeoFarage over 7 years
    @blazs I suppose it's for taking into account the distance from the Equator
  • scai
    scai over 7 years
    0.0000089? Try to avoid magic numbers, nobody will understand this.
  • Numan Karaaslan
    Numan Karaaslan over 7 years
    It is a short version of earth diameter and pi numbers in the code. not magic.
  • scai
    scai over 7 years
    It is still magic if nobody knows how to reproduce this number. Why don't you put the full calculation into your code?
  • Tushar Monirul
    Tushar Monirul about 7 years
    ok, what will be the direction? I mean if I want to add 50 meters, where it will be added? Right, left, up or down?
  • Sycraw
    Sycraw about 7 years
    shouldn't r_earth be 6371 km approximately? google.it/…*
  • nibot
    nibot about 7 years
    The earth is not perfectly spherical, so using a single value for "radius" is an approximation. Wikipedia says "distances from points on the surface to the center range from 6,353 km to 6,384 km". It also says "Several different ways of modeling the Earth as a sphere each yield a mean radius of 6,371 km" which indicates your value. Really, if this correction is significant in your application, you should be using a better algorithm anyway.
  • Muhammad Azeem
    Muhammad Azeem about 7 years
    1 degree in google map is equal to 111.32 Kilometer. 1Degree = 111.32KM. 1KM in Degree = 1 / 111.32 = 0.008983. 1M in Degree = 0.000008983.
  • chutsu
    chutsu almost 7 years
    You should have made a comment in your answer, putting it in the comments is not helpful.
  • metaColin
    metaColin almost 7 years
    @MuhammadAzeem your formula is not accurate. How many meters are represented within a degree changes depending on coordinate location and also depending on if it's a latitudinal degree or a longitudinal degree.
  • True Solutions
    True Solutions almost 7 years
    Other than putting the 0.0000089 in to a variable and naming it I think that is better than have a complete formula, as it will execute quicker than math logic for each calculation they doing. And if people want to know the maths then they would be googling that, not how to convert gps values.
  • Ahmed
    Ahmed over 6 years
    @Muhammad Azeem I have the same problem using this formula i can get one new lat and one new long in one direction, how to get the another direction ?
  • Amit Assaraf
    Amit Assaraf over 6 years
    For anyone who isn't sure the r_earth variable should be in meters and should be equal to approximately 6371000.0
  • Ilario
    Ilario about 6 years
    @Ahmed have you find a solution?
  • hmojtaba
    hmojtaba almost 6 years
    to calculate the new_longitude instead of latitude the new latitude should be used:new_longitude = longitude + (dx / r_earth) * (180 / pi) / cos(NEW_LATITUDE * pi/180);
  • Anna
    Anna over 4 years
    Please add some explanation to your answer.
  • Eyni Kave
    Eyni Kave over 4 years
    if you want move map object about 50 meter near the center of current map, then you can use this code with +,- numbers as replacement for +50
  • Hossein
    Hossein over 2 years
    Thanks a lot, fixed my problem. but I'd say it'd be much better if you could add some more explanatory information concerning the bits, especially the m and what is going on there.
  • Hossein
    Hossein over 2 years
    the magic number 0.00006279 you used can result in a huge offset. replace it with the value of this : earth_radius_in_km = 6378.137 coeff = (1 / ((2 * math.pi / 360) * earth_radius_in_km)) / 1000 blur_factor = meters * coeff # depending on the north, south use - or + on meters by applying this change, the offset for my shrunk from 36 meters to around 10 centimeters!
  • Curcuma_
    Curcuma_ almost 2 years
    @Ahmed it just works also with negative meters meters = Math.round((Math.random() - 0.5) * meters)