简体   繁体   English

将距离添加到 GPS 坐标

[英]Adding distance to a GPS coordinate

I'm trying to generate some points at random distances away from a fixed point using GPS.我正在尝试使用 GPS 在远离固定点的随机距离处生成一些点。

How can I add distance in meters to a GPS coordinate?如何将距离以米为单位添加到 GPS 坐标? I've looked at UTM to GPS conversion but is there a simpler method to achieve this?我看过 UTM 到 GPS 的转换,但有没有更简单的方法来实现这一点?

I'm working on Android platform just in case.我正在 Android 平台上工作以防万一。

Cheers, fgs干杯,fgs

  • P0(lat0,lon0) : initial position (unit : degrees ) P0(lat0,lon0) :初始位置(单位:
  • dx,dy : random offsets from your initial position in meters dx,dy :以为单位的初始位置的随机偏移量

You can use an approximation to compute the position of the randomized position:您可以使用近似值来计算随机位置的位置:

 lat = lat0 + (180/pi)*(dy/6378137)
 lon = lon0 + (180/pi)*(dx/6378137)/cos(lat0)

This is quite precise as long as the random distance offset is below 10-100 km只要随机距离偏移量低于 10-100 公里,这是非常精确的

Edit: of course in Java Math.cos() expects radians so do use Math.cos(Math.PI/180.0*lat0) if lat0 is in degrees as assumed above.编辑:当然在 Java 中 Math.cos() 需要弧度,所以如果 lat0 是上面假设的度数,请使用Math.cos(Math.PI/180.0*lat0)

To take a square I'm using this:要取一个正方形,我正在使用它:

 private double[] getBoundingBox(final double pLatitude, final double pLongitude, final int pDistanceInMeters) {

    final double[] boundingBox = new double[4];

    final double latRadian = Math.toRadians(pLatitude);

    final double degLatKm = 110.574235;
    final double degLongKm = 110.572833 * Math.cos(latRadian);
    final double deltaLat = pDistanceInMeters / 1000.0 / degLatKm;
    final double deltaLong = pDistanceInMeters / 1000.0 / degLongKm;

    final double minLat = pLatitude - deltaLat;
    final double minLong = pLongitude - deltaLong;
    final double maxLat = pLatitude + deltaLat;
    final double maxLong = pLongitude + deltaLong;

    boundingBox[0] = minLat;
    boundingBox[1] = minLong;
    boundingBox[2] = maxLat;
    boundingBox[3] = maxLong;

    return boundingBox;
}

This returns an array with 4 coordinates, with them you can make a square with your original point in center.这将返回一个具有 4 个坐标的数组,您可以使用它们制作一个以原始点为中心的正方形。

A detailed outline is given at http://www.movable-type.co.uk/scripts/latlong.html . http://www.movable-type.co.uk/scripts/latlong.html提供了详细的大纲。

If you, somewhere, need to interconvert longitude/latitude to UTM coordinates (the ones used in GPS) you may want to have a look at http://www.uwgb.edu/dutchs/UsefulData/UTMFormulas.htm如果您在某处需要将经度/纬度相互转换为 UTM 坐标(GPS 中使用的坐标),您可能需要查看http://www.uwgb.edu/dutchs/UsefulData/UTMFormulas.htm

If you want to go east or north or west or south you can use this:如果你想向东、向北、向西或向南走,你可以使用这个:

@SuppressLint("DefaultLocale")
public static double go_mock_loc(double xx_lat,double xx_long,double xx_dinstance,String Direction)
{
//  double xx_lat= 45.815005; 
//  double xx_long= 15.978501;

//  int xx_dinstance=500;

    int equator_circumference=6371000;
    int polar_circumference=6356800;

    double m_per_deg_long =  360 / polar_circumference;
    double rad_lat=(xx_lat* (Math.PI) / 180);
    double m_per_deg_lat = 360 / ( Math.cos(rad_lat) * equator_circumference);

    double deg_diff_long = xx_dinstance * m_per_deg_long;
    double deg_diff_lat  = xx_dinstance * m_per_deg_lat; 


    double xx_north_lat = xx_lat + deg_diff_long;
    //double xx_north_long= xx_long;
    double xx_south_lat = xx_lat - deg_diff_long;
    //double xx_south_long= xx_long;

    //double xx_east_lat = xx_lat;
    double xx_east_long= xx_long + deg_diff_lat;  
    //double xx_west_lat = xx_lat;
    double xx_west_long= xx_long - deg_diff_lat;

    if (Direction.toUpperCase().contains("NORTH")) {
        return xx_north_lat;
    } else if (Direction.toUpperCase().contains("SOUTH"))
    {
        return xx_south_lat;
    } else if (Direction.toUpperCase().contains("EAST"))
    {
        return xx_east_long;
    } else if (Direction.toUpperCase().contains("WEST"))
    {
        return xx_west_long;
    }
    else 
        return 0; 

}

I found that solution of @Bogdan Khrystov is very well.我发现@Bogdan Khrystov 的解决方案非常好。 So here is C# version of his solution.所以这是他的解决方案的 C# 版本。

public enum GeoDirection
{
   NORTH = 1, SOUTH = 2, EAST = 3, WEST = 4
}            
    
public static Tuple<double, double> AddDistanceInMeters(double latitude, double longitude, int distanceInMeters, GeoDirection direction)
{
  var equatorCircumference = 6371000;
  var polarCircumference = 6356800;
    
  var mPerDegLong = 360 / (double)polarCircumference;
  var radLat = latitude * Math.PI / 180;
  var mPerDegLat = 360 / (Math.Cos(radLat) * equatorCircumference);
    
  var degDiffLong = distanceInMeters * mPerDegLong;
  var degDiffLat = distanceInMeters * mPerDegLat;
    
  var xxNorthLat = latitude + degDiffLong;
  var xxSouthLat = latitude - degDiffLong;
  var xxEastLong = longitude + degDiffLat;
  var xxWestLong = longitude - degDiffLat;
    
  switch (direction)
  {
    case GeoDirection.NORTH:
       return new Tuple<double, double>(xxNorthLat, longitude);
    case GeoDirection.SOUTH:
       return new Tuple<double, double>(xxSouthLat, longitude);
    case GeoDirection.EAST:
       return new Tuple<double, double>(latitude, xxEastLong);
    case GeoDirection.WEST:
       return new Tuple<double, double>(latitude, xxWestLong);
    default:
       return null;
    }
}

This code splits the line between two coordinates in n segments.此代码将两个坐标之间的线分割为 n 段。 Replace the delta calculation by your fixed distance用您的固定距离替换增量计算

 @Override
public void split(Coordinates p1, Coordinates p2, int segments) {
    double φ1 = Math.toRadians(p1.getLat());
    double λ1 = Math.toRadians(p1.getLon());
    double φ2 = Math.toRadians(p2.getLat());
    double λ2 = Math.toRadians(p2.getLon());


    double xDelta = (φ2 - φ1) / segments;
    double yDelta = (λ2 - λ1) / segments;
    for (int i = 0; i < segments; i++){
        double x = φ1 + i * xDelta;
        double y = λ1 + i * yDelta;
        double xc = Math.toDegrees(x);
        double yc = Math.toDegrees(y);
        System.out.println(xc+","+yc);
    }
}

rewrite @Ersin Gülbahar answer in Kotlin:用 Kotlin 重写 @Ersin Gülbahar 的回答:

object LocationUtil {

enum class Direction {
    NORTH, SOUTH, EAST, WEST
}

fun addDistanceInMeters(
    latitude: Double,
    longitude: Double,
    distanceInMeters: Int,
    direction: Direction
): Pair<Double, Double> {
    val equatorCircumference = 6371000
    val polarCircumference = 6356800

    val mPerDegLong = (360 / polarCircumference.toDouble())
    val radLat = latitude * Math.PI / 180
    val mPerDegLat = 360 / (Math.cos(radLat) * equatorCircumference)

    val degDiffLong = distanceInMeters * mPerDegLong
    val degDiffLat = distanceInMeters * mPerDegLat

    val xxNorthLat = latitude + degDiffLong
    val xxSouthLat = latitude - degDiffLong
    val xxEastLong = longitude + degDiffLat
    val xxWestLong = longitude - degDiffLat

    return when (direction) {
        Direction.NORTH -> Pair(xxNorthLat, longitude)
        Direction.SOUTH -> Pair(xxSouthLat, longitude)
        Direction.EAST -> Pair(latitude, xxEastLong)
        Direction.WEST -> Pair(latitude, xxWestLong)
    }
}

} }

Combining answers from @Ersin Gülbahar and @Stéphane above, I came up with this solution in Flutter/Dart:结合上面@Ersin Gülbahar 和@Stéphane 的回答,我在 Flutter/Dart 中提出了这个解决方案:

import 'dart:math' as math;
enum Direction { north, south, east, west }

double moveCoordinate(
    double latitude, double longitude, double distanceToMoveInMeters, Direction directionToMove) {
  const earthEquatorRadius = 6378137;
  final latitudeOffset = (180 / math.pi) * (distanceToMoveInMeters / earthEquatorRadius);
  final longitudeOffset = (180 / math.pi) *
      (distanceToMoveInMeters / earthEquatorRadius) /
      math.cos(math.pi / 180 * latitude);

  switch (directionToMove) {
    case Direction.north:
      return latitude + latitudeOffset;
    case Direction.south:
      return latitude - latitudeOffset;
    case Direction.east:
      return longitude + longitudeOffset;
    case Direction.west:
      return longitude - longitudeOffset;
  }

  return 0;
}

This works, tested.这有效,经过测试。 The code is C# but you can easily change it to another language代码是 C#,但您可以轻松地将其更改为另一种语言

 private PointLatLng NewPositionBasedOnDistanceAngle(PointLatLng org, double distance, double bearing)
 {
    double rad = bearing * Math.PI / 180; //to radians
    double lat1 = org.Lat * Math.PI / 180; //to radians
    double lng1 = org.Lng * Math.PI / 180; //to radians
    double lat = Math.Asin(Math.Sin(lat1) * Math.Cos(distance / 6378137) + Math.Cos(lat1) * Math.Sin(distance / 6378137) * Math.Cos(rad));
    double lng = lng1 + Math.Atan2(Math.Sin(rad) * Math.Sin(distance / 6378137) * Math.Cos(lat1), Math.Cos(distance / 6378137) - Math.Sin(lat1) * Math.Sin(lat));
    return new PointLatLng(lat * 180 / Math.PI, lng * 180 / Math.PI); // to degrees  
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM