如何计算谷歌地图圆半径js到C#

时间:2012-10-31 09:07:33

标签: c#-4.0 google-maps-api-3

我知道如何使用javascript来计算半径,使用下面的代码

var center = new google.maps.LatLng(3.2987599, 102.6872022);
var latLng = new google.maps.LatLng(3.0987599, 101.6872022);
var distanceInMetres = google.maps.geometry.spherical.computeDistanceBetween(center, latLng);

但是如何将google.maps.geometry.spherical.computeDistanceBetween转换为C#函数?

4 个答案:

答案 0 :(得分:5)

2点之间的距离:(lat1,lon1)到(lat2,lon2)

distance = acos(
     cos(lat1 * (PI()/180)) *
     cos(lon1 * (PI()/180)) *
     cos(lat2 * (PI()/180)) *
     cos(lon2 * (PI()/180))
     +
     cos(lat1 * (PI()/180)) *
     sin(lon1 * (PI()/180)) *
     cos(lat2 * (PI()/180)) *
     sin(lon2 * (PI()/180))
     +
     sin(lat1 * (PI()/180)) *
     sin(lat2 * (PI()/180))
    ) * 3959

3959是英里的地球半径。将此值替换为 以KM为单位的半径(或任何其他单位),以便在同一单位上获得结果。

您可以通过与此worked example

进行比较来验证您的实施情况

答案 1 :(得分:1)

我已经编写了C#解决方案来计算转换距离

var distanceInMetres = google.maps.geometry.spherical.computeDistanceBetween(center,latLng);

进入C#。以下是我使用的代码。 6371是地球的半径。

    //Calculate distance earth between 2 coordinate point
    double e = lat * (Math.PI / 180);
    double f = lng * (Math.PI / 180);
    double g = lat2 * (Math.PI / 180);
    double h = lng2 * (Math.PI / 180);
    double i =
        (Math.Cos(e) * Math.Cos(g) * Math.Cos(f) * Math.Cos(h)
        + Math.Cos(e) * Math.Sin(f) * Math.Cos(g) * Math.Sin(h)
        + Math.Sin(e) * Math.Sin(g));
    double j = Math.Acos(i);
    double k = (6371 * j);  //Distance in KM

答案 2 :(得分:0)

有一个关于如何计算它的博客条目,没有任何带有来自纬度和经度的普通C#的API: LINK

答案 3 :(得分:0)

2纬度/长点之间的距离可以使用半正线公式计算,这里描述http://en.wikipedia.org/wiki/Haversine_formula

还有一个问题在stackoverflow上或多或少有同样的问题:Calculate distance between two latitude-longitude points? (Haversine formula)

相关问题