【问题标题】:How to get the bounds of a map control?如何获得地图控件的边界?
【发布时间】:2014-06-09 21:26:12
【问题描述】:

在Windows Phone c#上寻求帮助获取地图控件的边界,如地图顶部的纬度,地图底部的经度(可见区域),与经度相同,但左/右很明显。

我为“给我答案”之类的问题道歉,但我完全不知道该怎么做。

【问题讨论】:

    标签: c# windows windows-phone-8 windows-phone uwp-maps


    【解决方案1】:

    代码:

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

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

    【讨论】:

      【解决方案2】:

      我创建了一段代码,用于在 UWP 中查找 mapControl 的视点。

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

      享受

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

      如果用户旋转地图,这个函数缺少的是计算航向。有人对此有解决方案吗?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-09-12
        • 1970-01-01
        • 2019-11-29
        • 1970-01-01
        • 2014-04-26
        • 1970-01-01
        相关资源
        最近更新 更多