【问题标题】:Calculate Latitude and Longitude based on heading in a viewport根据视口中的航向计算纬度和经度
【发布时间】:2016-08-02 00:00:04
【问题描述】:

有没有人有根据航向计算视图的纬度和经度角的解决方案?

如果标题为 0,我有一个函数可以计算视图的 LatLng 角。但我希望找到一种方法来根据新标题计算角,例如,如果用户旋转地图。

我现在使用 Heading = 0 执行此操作的代码是这样的。

public GeoboundingBox GetBounds(MapControl map)
    {
        if(map.Center.Position.Latitude == 0) { return default(GeoboundingBox); }

        /*
         * resolution m/px = 15653.04 m/px * Cos(LatInRad) / 2^zoomLevel
         * 111325 m/deg
         */

        double latInRad = Math.Cos(map.Center.Position.Latitude * Math.PI / 180);
        double lngInRad = Math.Cos(map.Center.Position.Longitude * Math.PI / 180);

        double degreePerPixel = (156543.04 * latInRad * lngInRad) / (111325 * Math.Pow(2, map.ZoomLevel));

        double mHalfWidthInDegrees = map.ActualWidth * degreePerPixel / 0.89;
        double mHalfHeightInDegrees = map.ActualHeight * degreePerPixel / 1.65;

        double mNorth = map.Center.Position.Latitude + mHalfHeightInDegrees;
        double mWest = map.Center.Position.Longitude - mHalfWidthInDegrees;
        double mSouth = map.Center.Position.Latitude - mHalfHeightInDegrees;
        double mEast = map.Center.Position.Longitude + mHalfWidthInDegrees;

        GeoboundingBox mBounds = new GeoboundingBox(
            new BasicGeoposition()
            {
                Latitude = mNorth,
                Longitude = mWest
            },
            new BasicGeoposition()
            {
                Latitude = mSouth,
                Longitude = mEast
            });
      return mBounds;
 }

【问题讨论】:

    标签: c# google-maps uwp bing-maps uwp-maps


    【解决方案1】:

    获取可见地图区域的边界框最简单的解决方案是直接从地图控件中获取值。

    对于 Microsoft 的内置地图控件,您有 MapControl.GetLocationFromOffset 方法,该方法采用相对于控件的 Point 并返回该点的地理位置。

    mapControl.GetLocationFromOffset(
       new Point(0, 0),
       out upperLeftGeoPoint
    );
    mapControl.GetLocationFromOffset (
       new Point( mapControl.ActualWidth, 0 ), 
       out upperRightGeoPoint
    );
    mapControl.GetLocationFromOffset (
       new Point( 0, mapControl.ActualHeight ), 
       out bottomLeftGeoPoint
    );
    mapControl.GetLocationFromOffset (
       new Point( mapControl.ActualWidth, mapControl.ActualHeight ), 
       out bottomRightGeoPoint
    );
    

    请注意,如果该点超出地图控件的范围,该方法将引发异常。

    在您的情况下,您需要获取所有四个角的值,因为地图是旋转的。

    有关此方法的更多文档,see MSDN

    如果您使用的是第三方 XAML 映射控件,则可以使用等效的 ViewportPointToLocation 方法

    var northWestCorner = mapControl.ViewportPointToLocation( 
       new Point( 0, 0 )
    );
    var southEastCorner = mapControl.ViewportPointToLocation(
       new Point( mapControl.ActualWidth, mapControl.ActualHeight )
    );
    //analogous for north east, south west
    

    【讨论】:

      【解决方案2】:

      您似乎正在尝试计算地图的边界框。使用每个像素的度数将不起作用,因为该值会随纬度值而变化。而是看看这里的解决方案如何计算WP8.1中地图的边界框(这是Win10地图控件的基础)Get view bounds of a Map

      【讨论】:

      • 谢谢。这段代码似乎运行得最好,但是当用户在地图画布的顶部和底部之外导航时,我遇到了这段代码的一些问题。
      • 此代码解决了用户向上或向下导航太远时的问题。这就是 try/catch 的用途。如果一个点不在地图内,则它会捕捉到纬度的极限,即 85/-85。实际上它是 90/-90,但随后由于除以零而导致计算错误。
      • 我得到的问题是 catch 代码在 GetLocationFromOffset 上抛出异常。如果 Point(x,y) 坐标无效,则会抛出错误,并且 bottomRight 或 topLeft 将为空。我通过将这些代码部分放入 try{} catch{} 语句中解决了这个问题。这可能不适合一般用途,因为如果它们失败,我什么也不做。但我不希望我的用户在画布的顶部/底部获得任何数据。这只是为了防止应用程序崩溃。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-10
      • 2012-08-16
      • 1970-01-01
      • 2020-03-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多