2016-08-02 42 views
1

有没有人有基于标题计算视图的纬度和经度角的解决方案?根据视口中的标题计算经度和纬度

我有一个函数可以在标题为0的情况下计算视图的LatLng角点。但是我希望找到一种方法来计算基于新标题的角点,例如用户旋转地图。

我现在用代码= 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; 
} 

回答

0

它看起来像你试图计算地图的边界框。由于此值随纬度值而变化,因此每像素使用度数不起作用。相反,请看这里的解决方案,了解如何计算WP8.1中的地图边界框(这是Win10地图控件的基础)Get view bounds of a Map

+0

谢谢。这段代码看起来效果最好,但是当用户在地图画布的顶部和底部之外导航时,我遇到了一些与此代码有关的问题。 – Startail

+0

这段代码解决了用户导航过度或过低的问题。这就是尝试/捕获的目的。如果一个点不在地图内,它会捕捉到85/-85的纬度限制。实际上它是90/-90,但是由于除以零而计算出错。 – rbrundritt

+0

我得到的问题是catch代码抛出GetLocationFromOffset异常。如果Point(x,y)坐标无效,则会抛出错误,bottomRight或topLeft将为空。我已经解决了这个问题,将代码的这些部分放在try {} catch {}语句中。这可能不适合一般用途,因为如果它们失败,我什么也不做。但我不希望我的用户在画布的顶部/底部获取任何数据。这只是为了防止应用程序崩溃。 – Startail

2

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

对于Microsoft的内置Map控件,您有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 
+0

不客气:-) –