简体   繁体   English

如何获取地图控件的边界?

[英]How to get the bounds of a map control?

Looking for help getting bounds of a map control on Windows Phone c#, as in the latitude of the top of the map, longitude of the bottom of the map (Visible area), the same thing with longitude, but left/right obviously.寻求帮助在 Windows Phone c# 上获取地图控件的边界,如地图顶部的纬度,地图底部的经度(可见区域),与经度相同,但显然是左/右。

I apologise for the nooby "Just give me the answer" kind of question, but I have absolutely no idea how to do this.我为菜鸟“给我答案”之类的问题道歉,但我完全不知道该怎么做。

Code:代码:

public LocationRectangle GetVisibleMapArea(Map mMap)
{
    GeoCoordinate mCenter = mMap.Center;
    Point pCenter = mMap.ConvertGeoCoordinateToViewportPoint(mCenter);
    GeoCoordinate topLeft = MapVieMode.ConvertViewportPointToGeoCoordinate(new Point(0, 0));
    GeoCoordinate bottomRight = MapVieMode.ConvertViewportPointToGeoCoordinate(new Point(MapVieMode.ActualWidth, MapVieMode.ActualHeight));

    if (topLeft != null && bottomRight != null)
    {
        Point pNW = new Point(pCenter.X - mMap.ActualWidth / 2, pCenter.Y - mMap.ActualHeight / 2);
        Point pSE = new Point(pCenter.X + mMap.ActualWidth / 2, pCenter.Y + mMap.ActualHeight / 2);
        if (pNW != null && pSE != null)
        {
            GeoCoordinate gcNW = mMap.ConvertViewportPointToGeoCoordinate(pNW);
            GeoCoordinate gcSE = mMap.ConvertViewportPointToGeoCoordinate(pSE);
            return new LocationRectangle(gcNW, gcSE);
        }
    }

    return null;
}

Taken from the example here: http://code.msdn.microsoft.com/wpapps/Windows-Phone-8-Map-0ca7bd6c取自此处的示例: http : //code.msdn.microsoft.com/wpapps/Windows-Phone-8-Map-0ca7bd6c

I've created a piece of code that finds the viewpoint of a mapControl in UWP.我创建了一段代码,用于在 UWP 中查找 mapControl 的视点。

Original source at https://social.msdn.microsoft.com/Forums/windowsapps/en-us/4e5398de-ec50-46df-84d5-087dcaa20924/wp8-map-viewchanged-and-viewchanging-events-extents?forum=wpdevelop原始来源位于https://social.msdn.microsoft.com/Forums/windowsapps/en-us/4e5398de-ec50-46df-84d5-087dcaa20924/wp8-map-viewchanged-and-viewchanged-events-extents?forum=wpdevelop

Enjoy享受

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

        double degreePerPixel = (156543.04 * Math.Cos(map.Center.Position.Latitude * Math.PI / 180)) / (111325 * Math.Pow(2, map.ZoomLevel));

        double mHalfWidthInDegrees = map.ActualWidth * degreePerPixel / 0.9;
        double mHalfHeightInDegrees = map.ActualHeight * degreePerPixel / 1.7;

        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
            });

        Debug.WriteLine("New Bounds: NW = " + mNorth + ":" + mWest + " SE = " + mSouth + ":" + mEast);

        return mBounds;
    }

Something this function lack is calculating the heading if the user spins the map.如果用户旋转地图,此函数缺少的是计算航向。 Does someone have a solution for this?有人对此有解决方案吗?

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

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