有没有人有基于标题计算视图的纬度和经度角的解决方案?根据视口中的标题计算经度和纬度
我有一个函数可以在标题为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;
}
谢谢。这段代码看起来效果最好,但是当用户在地图画布的顶部和底部之外导航时,我遇到了一些与此代码有关的问题。 – Startail
这段代码解决了用户导航过度或过低的问题。这就是尝试/捕获的目的。如果一个点不在地图内,它会捕捉到85/-85的纬度限制。实际上它是90/-90,但是由于除以零而计算出错。 – rbrundritt
我得到的问题是catch代码抛出GetLocationFromOffset异常。如果Point(x,y)坐标无效,则会抛出错误,bottomRight或topLeft将为空。我已经解决了这个问题,将代码的这些部分放在try {} catch {}语句中。这可能不适合一般用途,因为如果它们失败,我什么也不做。但我不希望我的用户在画布的顶部/底部获取任何数据。这只是为了防止应用程序崩溃。 – Startail