简体   繁体   English

根据视口中的标题计算纬度和经度

[英]Calculate Latitude and Longitude based on heading in a viewport

Does anyone have a solution for calculating the Latitude and Longitude corners of a view based on heading? 有没有人有基于标题计算视图纬度和经度角的解决方案?

I have a function that calculates the LatLng corners of a view if heading is 0. But I wish to find a way to calculate the corners based on a new heading, if the user spins the map for example. 如果航向为0,我有一个计算视图的LatLng角的函数。但是,我希望找到一种方法,例如,如果用户旋转地图,则根据新的航向来计算角。

The code I have now that does this with Heading = 0 is this. 我现在使用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;
 }

The simplest solution to get the bounding box of the visible map area is to get the values directly from the map control. 获取可见地图区域边界框的最简单解决方案是直接从地图控件获取值。

For the built-in Map control by Microsoft you have the MapControl.GetLocationFromOffset method which takes a Point relative to the control and returns the geographic location at that point. 对于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
);

Be aware that the method will throw an exception in case the point is outside the range of the map control. 请注意,如果该点超出地图控件的范围,则该方法将引发异常。

In your case, you will need to get the values for all four corners, because it the map is rotated. 对于您的情况,您将需要获取所有四个角的值,因为它将旋转地图。

For more documentation of this method, see MSDN . 有关此方法的更多文档, 请参见MSDN

If you are using the third-party XAML Map Control, you have the equivalent ViewportPointToLocation method 如果使用第三方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

It looks like you are trying to calculate the bounding box of the map. 您似乎正在尝试计算地图的边界框。 Using degree per pixel won't work as this value changes with the latitude value. 由于此值随纬度值变化,因此无法使用每像素的度数。 Instead take a look at the solution here on how to calculate the bounding box of a map in WP8.1 (this basis to the Win10 map control) Get view bounds of a Map 而是在这里查看如何在WP8.1中计算地图边界框的解决方案(此基础是Win10地图控件的基础) 获取地图的视图边界

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM