C# 相当于 Google Maps API 的computeArea

发布于 2024-12-08 04:01:44 字数 280 浏览 0 评论 0原文

Google Maps Api 有一个 google.maps.geometry.spherical.computeArea< /a> 方法。 我怎样才能用 C# 编写它的等价物? (公式是什么)

我有一组经纬度值,我需要计算封闭多边形的面积(以米为单位)。

示例代码受到高度赞赏。

Google Maps Api has a google.maps.geometry.spherical.computeArea method.
How can I write its equivalent in C#? (What will be the formula)

I have a set of lat long values for which I need to calculate area (in meters) of the enclosed polygon.

Sample code is highly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

白首有我共你 2024-12-15 04:01:44

很抱歉为旧问题添加答案,但如果其他人正在寻找快速答案:

private const double EARTH_RADIUS = 6378137;

public class LatLng
{
    public double latitude { get; private set; }
    public double longitude { get; private set; }

    public LatLng(double latitude, double longitude)
    {
        this.latitude = latitude;
        this.longitude = longitude;
    }
}

public static double computeArea(List<LatLng> path)
{
    return Math.Abs(computeSignedArea(path));
}

private static double computeSignedArea(List<LatLng> path, double radius = EARTH_RADIUS)
{
    int size = path.Count;
    if (size < 3) { return 0; }
    double total = 0;
    LatLng prev = path[size - 1];
    double prevTanLat = Math.Tan((Math.PI / 2 - toRadians(prev.latitude)) / 2);
    double prevLng = toRadians(prev.longitude);
    // For each edge, accumulate the signed area of the triangle formed by the North Pole
    // and that edge ("polar triangle").
    foreach (LatLng point in path)
    {
        double tanLat = Math.Tan((Math.PI / 2 - toRadians(point.latitude)) / 2);
        double lng = toRadians(point.longitude);
        total += polarTriangleArea(tanLat, lng, prevTanLat, prevLng);
        prevTanLat = tanLat;
        prevLng = lng;
    }
    return total * (radius * radius);
}

private static double polarTriangleArea(double tan1, double lng1, double tan2, double lng2)
{
    double deltaLng = lng1 - lng2;
    double t = tan1 * tan2;
    return 2 * Math.Atan2(t * Math.Sin(deltaLng), 1 + t * Math.Cos(deltaLng));
}

private static double toRadians(double input)
{
    return input * Math.PI / 180;
}

参考https://github.com/googlemaps

Sorry for adding an answer to an old question, but if someone else is looking for a quickly answer:

private const double EARTH_RADIUS = 6378137;

public class LatLng
{
    public double latitude { get; private set; }
    public double longitude { get; private set; }

    public LatLng(double latitude, double longitude)
    {
        this.latitude = latitude;
        this.longitude = longitude;
    }
}

public static double computeArea(List<LatLng> path)
{
    return Math.Abs(computeSignedArea(path));
}

private static double computeSignedArea(List<LatLng> path, double radius = EARTH_RADIUS)
{
    int size = path.Count;
    if (size < 3) { return 0; }
    double total = 0;
    LatLng prev = path[size - 1];
    double prevTanLat = Math.Tan((Math.PI / 2 - toRadians(prev.latitude)) / 2);
    double prevLng = toRadians(prev.longitude);
    // For each edge, accumulate the signed area of the triangle formed by the North Pole
    // and that edge ("polar triangle").
    foreach (LatLng point in path)
    {
        double tanLat = Math.Tan((Math.PI / 2 - toRadians(point.latitude)) / 2);
        double lng = toRadians(point.longitude);
        total += polarTriangleArea(tanLat, lng, prevTanLat, prevLng);
        prevTanLat = tanLat;
        prevLng = lng;
    }
    return total * (radius * radius);
}

private static double polarTriangleArea(double tan1, double lng1, double tan2, double lng2)
{
    double deltaLng = lng1 - lng2;
    double t = tan1 * tan2;
    return 2 * Math.Atan2(t * Math.Sin(deltaLng), 1 + t * Math.Cos(deltaLng));
}

private static double toRadians(double input)
{
    return input * Math.PI / 180;
}

Reference https://github.com/googlemaps

被翻牌 2024-12-15 04:01:44

你自己尝试过什么?

有一些非常相似的问题有详细的答案,所以最好参考它们:

What have you tried yourself?

There are some very similar questions with detailed answers, so it's best to refer to them:

〗斷ホ乔殘χμё〖 2024-12-15 04:01:44

您可以从维基百科获取公式。

You can get the formula from Wikipedia.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文