Calculating new longitude, latitude from old + n meters

AlgorithmGeolocation

Algorithm Problem Overview


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.

Algorithm Solutions


Solution 1 - Algorithm

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 - Algorithm

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 - Algorithm

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 - Algorithm

Have you checked out: https://stackoverflow.com/questions/1125144/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

Solution 5 - Algorithm

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 6 - Algorithm

if you don't have to be very exact then: each 10000 meters is about 0.1 for latitude and longitude. for example I want to load locations 3000 meters around point_A from my database:

double newMeter =  3000 * 0.1 / 10000;
double lat1 = point_A.latitude - newMeter;
double lat2 = point_A.latitude + newMeter;
double lon1 = point_A.longitude - newMeter;
double lon1 = point_A.longitude + newMeter;
Cursor c = mDb.rawQuery("select * from TABLE1  where lat >= " + lat1 + " and lat <= " + lat2 + " and lon >= " + lon1 + " and lon <= " + lon2 + " order by id", null);

Solution 7 - Algorithm

Working Python code to offset coordinates by 10 metres.

def add_blur(lat, long):
meters = 10
blur_factor = meters * 0.000006279
new_lat = lat + blur_factor
new_long = long + blur_factor / math.cos(lat * 0.018)
return new_lat, new_long

Solution 8 - Algorithm

public double MeterToDegree(double meters, double latitude)
{
    return meters / (111.32 * 1000 * Math.Cos(latitude * (Math.PI / 180)));
}

Solution 9 - Algorithm

See from Official Google Maps Documentation (link below) as they solve on easy/simple maps the problems with distance by countries :)

I recommended this solution to easy/simply solve issue with boundaries that you can know which area you're solving the problem with boundaries (not recommended globally)

Note:

Latitude lines run west-east and mark the position south-north of a point. Lines of latitude are called parallels and in total there are 180 degrees of latitude. The distance between each degree of latitude is about 69 miles (110 kilometers).

The distance between longitudes narrows the further away from the equator. The distance between longitudes at the equator is the same as latitude, roughly 69 miles (110 kilometers) . At 45 degrees north or south, the distance between is about 49 miles (79 kilometers). The distance between longitudes reaches zero at the poles as the lines of meridian converge at that point.

Original source 1 Original source 2 enter image description here

Official Google Maps Documentation: Code Example: Autocomplete Restricted to Multiple Countries

See the part of their code how they solve problem with distance center + 10 kilometers by +/- 0.1 degree

function initMap(): void {
  const map = new google.maps.Map(
    document.getElementById("map") as HTMLElement,
    {
      center: { lat: 50.064192, lng: -130.605469 },
      zoom: 3,
    }
  );
  const card = document.getElementById("pac-card") as HTMLElement;
  map.controls[google.maps.ControlPosition.TOP_RIGHT].push(card);
  const center = { lat: 50.064192, lng: -130.605469 };

  // Create a bounding box with sides ~10km away from the center point
  const defaultBounds = {
    north: center.lat + 0.1,
    south: center.lat - 0.1,
    east: center.lng + 0.1,
    west: center.lng - 0.1,
  };

  const input = document.getElementById("pac-input") as HTMLInputElement;
  const options = {
    bounds: defaultBounds,
    componentRestrictions: { country: "us" },
    fields: ["address_components", "geometry", "icon", "name"],
    origin: center,
    strictBounds: false,
    types: ["establishment"],
  };

Solution 10 - Algorithm

This is what I did in VBA that seems to be working for me. Calculation is in feet not meters though

Public Function CalcLong(OrigLong As Double, OrigLat As Double, DirLong As String, DirLat As String, DistLong As Double, DistLat As Double)
    Dim FT As Double
    Dim NewLong, NewLat As Double
    FT = 1 / ((2 * WorksheetFunction.Pi / 360) * 20902230.971129)
    
    If DirLong = "W" Then
        NewLat = CalcLat(OrigLong, OrigLat, DirLong, DirLat, DistLong, DistLat)
        NewLong = OrigLong - ((FT * DistLong) / Cos(NewLat * (WorksheetFunction.Pi / 180)))
        CalcLong = NewLong
    Else
        NewLong = OrigLong + ((FT * DistLong) / Math.Cos(CalcLat(OrigLong, OrigLat, DirLong, DirLat, DistLong, DistLat) * (WorksheetFunction.Pi / 180)))
        CalcLong = NewLong
    End If
    
End Function


Public Function CalcLat(OrigLong As Double, OrigLat As Double, DirLong As String, DirLat As String, DistLong As Double, DistLat As Double) As Double
    Dim FT As Double
    Dim NewLat As Double
    
    FT = 1 / ((2 * WorksheetFunction.Pi / 360) * 20902230.971129)
    
    If DirLat = "S" Then
        NewLat = (OrigLat - (FT * DistLat))
        CalcLat = NewLat
    Else
        NewLat = (OrigLat + (FT * DistLat))
        CalcLat = NewLat
    End If
    
End Function

Solution 11 - Algorithm

Posting this method for sake of completeness.

Use this method "as it is" to:

  • Move any (lat,long) point by given meters in either axis.

Python method to move any point by defined meters.

def translate_latlong(lat,long,lat_translation_meters,long_translation_meters):
    ''' method to move any lat,long point by provided meters in lat and long direction.
    params :
        lat,long: lattitude and longitude in degrees as decimal values, e.g. 37.43609517497065, -122.17226450150885
        lat_translation_meters: movement of point in meters in lattitude direction.
                                positive value: up move, negative value: down move
        long_translation_meters: movement of point in meters in longitude direction.
                                positive value: left move, negative value: right move
        '''
    earth_radius = 6378.137

    #Calculate top, which is lat_translation_meters above
    m_lat = (1 / ((2 * math.pi / 360) * earth_radius)) / 1000;  
    lat_new = lat + (lat_translation_meters * m_lat)

    #Calculate right, which is long_translation_meters right
    m_long = (1 / ((2 * math.pi / 360) * earth_radius)) / 1000;  # 1 meter in degree
    long_new = long + (long_translation_meters * m_long) / math.cos(latitude * (math.pi / 180));
    
    return lat_new,long_new

Solution 12 - Algorithm

var meters = 50;
var coef = meters * 0.0000089;
var new_lat = map.getCenter().lat.apply() + coef;
var new_long = map.getCenter().lng.apply() + coef / Math.cos(new_lat * 0.018);
map.setCenter({lat:new_lat, lng:new_long});

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBenjamin Udink ten CateView Question on Stackoverflow
Solution 1 - AlgorithmnibotView Answer on Stackoverflow
Solution 2 - AlgorithmNuman KaraaslanView Answer on Stackoverflow
Solution 3 - AlgorithmsstenView Answer on Stackoverflow
Solution 4 - AlgorithmRyan TernierView Answer on Stackoverflow
Solution 5 - AlgorithmmshwfView Answer on Stackoverflow
Solution 6 - Algorithmfarhad.kargaranView Answer on Stackoverflow
Solution 7 - AlgorithmPTTView Answer on Stackoverflow
Solution 8 - AlgorithmM KomaeiView Answer on Stackoverflow
Solution 9 - AlgorithmMiky A. MikolajView Answer on Stackoverflow
Solution 10 - AlgorithmOleksandr StorcheusView Answer on Stackoverflow
Solution 11 - AlgorithmsandeepsignView Answer on Stackoverflow
Solution 12 - AlgorithmEyni KaveView Answer on Stackoverflow