【问题标题】:How to calculate map bounds without javascript如何在没有 javascript 的情况下计算地图边界
【发布时间】:2013-12-22 08:56:29
【问题描述】:

我有一个坐标列表,将被视为地图的中心点。

我需要仅使用 C#(无 javascript 无地图对象)计算缩放级别 16 的地图边界。

这可能吗?

请指教。

【问题讨论】:

  • 您只需要数学,这与语言无关。
  • 是的,我知道,但你有数学的例子吗?请指教
  • 只需在循环中找到 x,y 的最小值和最大值。这给了你边界矩形。不过,不确定您的意思是“缩放级别 16”。
  • 我假设“缩放级别 16”是谷歌地图的缩放级别。你想要做的意味着非常复杂的地图和信息,你只能在 JavaScript 中获得。 为什么要在 c# 中执行此操作?
  • 我正在做一个网站清理项目,特定网站使用谷歌地图向服务器发送请求,其中包括地图边界。我确实有中心点列表,需要为每个点生成边界。

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


【解决方案1】:

这是我想要的东西。我还没有写过这门课,我在这里的某个地方找到了,我不记得了,所以功劳归于 X 人!提供纬度和经度以及以公里为单位的半径给出一个边界框。有点像 GMap 在视口边界上的作用。

            public class GlobalMercator
            {
                public class MapPoint
                {
                    public double Longitude { get; set; } // In Degrees
                    public double Latitude { get; set; } // In Degrees
                }

                public class BoundingBox
                {
                    public MapPoint MinPoint { get; set; }
                    public MapPoint MaxPoint { get; set; }
                }

                // Semi-axes of WGS-84 geoidal reference
                private const double WGS84_a = 6378137.0; // Major semiaxis [m]
                private const double WGS84_b = 6356752.3; // Minor semiaxis [m]

                // 'halfSideInKm' is the half length of the bounding box you want in kilometers.
                public static BoundingBox GetBoundingBox(MapPoint point, double halfSideInKm)
                {
                    // Bounding box surrounding the point at given coordinates,
                    // assuming local approximation of Earth surface as a sphere
                    // of radius given by WGS84
                    var lat = Deg2rad(point.Latitude);
                    var lon = Deg2rad(point.Longitude);
                    var halfSide = 1000 * halfSideInKm;

                    // Radius of Earth at given latitude
                    var radius = WGS84EarthRadius(lat);
                    // Radius of the parallel at given latitude
                    var pradius = radius * Math.Cos(lat);

                    var latMin = lat - halfSide / radius;
                    var latMax = lat + halfSide / radius;
                    var lonMin = lon - halfSide / pradius;
                    var lonMax = lon + halfSide / pradius;

                    return new BoundingBox
                    {
                        MinPoint = new MapPoint { Latitude = Rad2deg(latMin), Longitude = Rad2deg(lonMin) },
                        MaxPoint = new MapPoint { Latitude = Rad2deg(latMax), Longitude = Rad2deg(lonMax) }
                    };
                }

                // degrees to radians
                private static double Deg2rad(double degrees)
                {
                    return Math.PI * degrees / 180.0;
                }

                // radians to degrees
                private static double Rad2deg(double radians)
                {
                    return 180.0 * radians / Math.PI;
                }

                // Earth radius at a given latitude, according to the WGS-84 ellipsoid [m]
                private static double WGS84EarthRadius(double lat)
                {
                    // http://en.wikipedia.org/wiki/Earth_radius
                    var An = WGS84_a * WGS84_a * Math.Cos(lat);
                    var Bn = WGS84_b * WGS84_b * Math.Sin(lat);
                    var Ad = WGS84_a * Math.Cos(lat);
                    var Bd = WGS84_b * Math.Sin(lat);
                    return Math.Sqrt((An * An + Bn * Bn) / (Ad * Ad + Bd * Bd));
                }  
            }   

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-13
    相关资源
    最近更新 更多