GPS计算器转换,计算新点的纬度/经度值

时间:2014-09-05 13:03:44

标签: c# gps latitude-longitude

这是我遇到的问题:

我在C#中加载图片。在该图像上,我必须插入2个点:点A和点B,通过在随机的位置上单击鼠标。

点A有从电路中读取的电线(Xa,Ya),我需要手动插入GPS坐标(LatitudeA和LongtudeA)。

B点有自己的绳索(Xb,Yb)也可以从程序中读取,我需要手动插入GPS坐标(LatitudeB和LongtudeB)。

接下来是主要问题:在屏幕上的每次下一次点击中,我必须知道该点的GPS线。此外,C i还有(Xc,Yc)。

这是我的ComputeLatitudeAndLogitude方法,但它似乎并不完美。我需要街道尺寸。

示例:

A(487,361,45.252464,19.850337)

B(1167,397,45.252026,19.853990)

C(810,513,???,???);结果应为C(810,513,45.251592,19.852075)

请随时与我联系,以便我们解决问题,mymailis hladnopivo1990@gmail.com

public void ComputeLatitudeAndLogitud (Wpf_Maps.Point point)
{
        int diffX = pointA.X - pointB.X;
        int diffY = pointA.Y - pointB.Y;
        double diffLatitude = pointA.Latitude - pointB.Latitude;
        double diffLongitude = pointA.Longitude - pointB.Longitude;
        double latitudePerPixel = Math.Abs(diffLatitude / diffX);
        double longitudePerPixel = Math.Abs(diffLongitude / diffY);

        int diffXforA = pointA.X - point.X;
        int diffYforA = pointA.Y - point.Y;
        int diffXforB = pointB.X - point.X;
        int diffYforB = pointB.Y - point.Y;

        double newLatitudeFromA = pointA.Latitude + (diffXforA * latitudePerPixel);
        double newLatitudeFromB = pointB.Latitude + (diffXforB * latitudePerPixel);

        double newLongitudeFromA = pointA.Longitude + (diffYforA * longitudePerPixel);
        double newLongitudeFromB = pointB.Longitude + (diffYforB * longitudePerPixel);

        point.Latitude = (newLatitudeFromA + newLatitudeFromB) / 2;
        point.Longitude = (newLongitudeFromA + newLongitudeFromB) / 2;
    }

4 个答案:

答案 0 :(得分:1)

根据您需要覆盖的距离,线性外推不会太好;地球不平坦,纬度距离随经度而变化。

一个近似值是您计算Great-circle distance的球体。

(GPS)坐标通常相对于地球的(非球形)模型记录,WGS-84 椭球是今天最常见的。因此,为了获得最大精度,您必须根据相应的参考模型计算距离。

此外,如果图像的参考模型与GPS坐标的参考模型不同,则可能需要两个以上的参考点来确定精确的映射。

答案 1 :(得分:0)

我认为pointA和pointB位于地图的对角,A位于左下角(或左上角?)...意味着每个点C都在点A的上方和右侧。

尝试这种简化:

    public void ComputeLatitudeAndLogitud (Wpf_Maps.Point point)
    {
        int diffX = pointA.X - pointB.X;
        int diffY = pointA.Y - pointB.Y;
        double diffLatitude = pointA.Latitude - pointB.Latitude;
        double diffLongitude = pointA.Longitude - pointB.Longitude;
        double latitudePerPixel = Math.Abs(diffLatitude / diffX);
        double longitudePerPixel = Math.Abs(diffLongitude / diffY);

        int diffXforC = point.X - pointA.X;
        int diffYforC = point.Y - pointA.Y;

        point.Latitude = pointA.Latitude + (diffXforC * latitudePerPixel);
        point.Longitude = pointA.Longitude + (diffYforC * longitudePerPixel);
   }

答案 2 :(得分:0)

这是我的完整代码。我在那里有三个测试案例。第一个是pointC随机的地方,第二个是pointC匹配pointA,第三个是pointC匹配pointB。

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            ComputeLatitudeAndLongitude(new MyPoint(0, 0, 10, 10), new MyPoint(100, 100, 40, 40), new MyPoint(20, 40));
            ComputeLatitudeAndLongitude(new MyPoint(0, 0, 10, 10), new MyPoint(100, 100, 40, 40), new MyPoint(0, 0));
            ComputeLatitudeAndLongitude(new MyPoint(0, 0, 10, 10), new MyPoint(100, 100, 40, 40), new MyPoint(100, 100));
        }

        public void ComputeLatitudeAndLongitude(MyPoint pointA, MyPoint pointB, MyPoint pointC)
        {
            int diffX = pointA.X - pointB.X;
            int diffY = pointA.Y - pointB.Y;
            double diffLatitude = pointA.Latitude - pointB.Latitude;
            double diffLongitude = pointA.Longitude - pointB.Longitude;
            double latitudePerPixel = Math.Abs(diffLatitude / diffX);
            double longitudePerPixel = Math.Abs(diffLongitude / diffY);

            int diffXforC = pointC.X - pointA.X;
            int diffYforC = pointC.Y - pointA.Y;

            pointC.Latitude = pointA.Latitude + (diffXforC * latitudePerPixel);
            pointC.Longitude = pointA.Longitude + (diffYforC * longitudePerPixel);

            LogResults(String.Format("pointA X:{0} Y:{1} Lat:{2} Long:{3}", pointA.X, pointA.Y, pointA.Latitude, pointA.Longitude), true);
            LogResults(String.Format("pointB X:{0} Y:{1} Lat:{2} Long:{3}", pointB.X, pointB.Y, pointB.Latitude, pointB.Longitude), true);
            LogResults(String.Format("pointC X:{0} Y:{1} Lat:{2} Long:{3}", pointC.X, pointC.Y, pointC.Latitude, pointC.Longitude), true);
            LogResults(String.Empty, true);
        }

        public void LogResults(string message, bool insertNewline)
        {
            txtResults.Text += message + (insertNewline ? Environment.NewLine : String.Empty);
        }
    }

    public class MyPoint
    {
        public int X;
        public int Y;
        public double Latitude = 0;
        public double Longitude = 0;

        public MyPoint(int x, int y)
        {
            X = x;
            Y = y;
        }

        public MyPoint(int x, int y, double latitude, double longitude) : this(x, y)
        {
            Latitude = latitude;
            Longitude = longitude;
        }
    }

结果:

pointA X:0 Y:0 Lat:10 Long:10
pointB X:100 Y:100 Lat:40 Long:40
pointC X:20 Y:40 Lat:16 Long:22  // C.X is 20% of the way from A.X to B.X, so C.Lat is 20% of the way from A.Lat to B.Lat, Y/Long are 40%

pointA X:0 Y:0 Lat:10 Long:10
pointB X:100 Y:100 Lat:40 Long:40
pointC X:0 Y:0 Lat:10 Long:10  // C.X = A.X and C.Y = A.Y, therefore C.Lat and C.Long = A.Lat and A.Long

pointA X:0 Y:0 Lat:10 Long:10
pointB X:100 Y:100 Lat:40 Long:40
pointC X:100 Y:100 Lat:40 Long:40  // C.X = B.X and C.Y = B.Y, therefore C.Lat and C.Long = B.Lat and B.Long

答案 3 :(得分:0)

上面写的代码无法完成。如果我们想要获得高精度(至少6位数:n,mmmmmm),那么常量EARTH_RADIUS必须在计算功能中实现。下一个代码将按比例缩放适当的纬度/经度,因此当我们放置A点和B点的(X,Y)线时,计算出的点C将匹配指示点A和B.

public void ComputeLatitudeAndLogitude(Wpf_Maps.Point point){

        double diffLatAB = pointB.Latitude - pointA.Latitude;
        double diffLonAB = pointB.Longitude - pointA.Longitude;

        int diffXAB = pointB.X - pointA.X;
        int diffYAB = pointB.Y - pointA.Y;

        int diffXAC = point.X - pointA.X;
        int diffYAC = point.Y - pointA.Y;

        point.Latitude = diffLatAB / diffXAB * diffXAC + pointA.Latitude;
        point.Longitude = diffLonAB / diffYAB * diffYAC + pointA.Longitude;
   }