计算经纬度到公里 [英] Compute Latitude and Longitude to Kilometer

查看:123
本文介绍了计算经纬度到公里的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用经度和纬度计算从 Destination1 Destination2 的千米距离

I'm trying to calculate my Kilometer distance from my Destination1 to Destination2 using latitude and longitude

目的地1(宿雾市PH)的纬度为1017.8590,经度为12353.4304

Destination1(Cebu City PH) has Latitude: 1017.8590 and Longitude: 12353.4304

目的地2(Talisay PH)的纬度为1015.6445,经度为12350.0404

Destination2(Talisay PH) has Latitude: 1015.6445 and Longitude: 12350.0404

我的输出是: 299.28607720457882 KM

代码:

double lat1 = Convert.ToDouble(txtLat1.Text.ToString());
double lat2 = Convert.ToDouble(txtLat2.Text.ToString());
double lon1 = Convert.ToDouble(txtLong1.Text.ToString());
double lon2 = Convert.ToDouble(txtLong2.Text.ToString());

        var R = 6378.137; // Radius of earth in KM
        var dLat = (lat2 - lat1) * Math.PI / 180;
        var dLon = (lon2 - 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));
        string x = (R * c).ToString();
        var d = (R * c);

我尝试使用 http://www.entfernungsrechner.net/en /distance/city/1717512/city/1683881 此网站可以计算出该目的地的公里数,输出为 12.2公里

I try using http://www.entfernungsrechner.net/en/distance/city/1717512/city/1683881 this website to calculate the Kilometer of that Destination and the output is 12.2 km

推荐答案

好吧,对于初学者来说,它的驱动力为12.2公里.与上面的Haversine公式输出的结果不同.

Well, for starters it says 12.2KM as you would drive. Which is not going to be the same output as your Haversine formula you have above.

您应该尝试将小数点放在正确的位置.我得到了更合理的4.45公里的结果.

You should try putting the decimals in the right spots. I get a more reasonable result of 4.45 Kilometers.

https://dotnetfiddle.net/cs5Ppq

public class Program
{
    public static void Main()
    {
        double lat1 = 10.178590;
        double lat2 = 10.156445;
        double lon1 = 123.500404;
        double lon2 = 123.534304;

        var R = 6378.137; // Radius of earth in KM
        var dLat = (lat2 - lat1) * Math.PI / 180;
        var dLon = (lon2 - 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));
        string x = (R * c).ToString();
        var d = (R * c);
        Console.WriteLine(d);
    }
}

注意:您可以对照此 http://andrew.hedges.name/experiments/Haversine/工具

Note: you can check your results against this http://andrew.hedges.name/experiments/haversine/ tool

如果您想要最准确的数字,因为Haversine公式可能会低估和高估地球极端处的距离,所以您想使用Vincety解决方案.这不是为了弱者: http://www.movable-type.co.uk/scripts/latlong -vincenty.html

If you want the most possibly accurate number, as the Haversine formula can underestimate and overestimate distances at the extremes on the earth, you want to use the Vincety solution. This is not for the weak of heart: http://www.movable-type.co.uk/scripts/latlong-vincenty.html

这篇关于计算经纬度到公里的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆