2013-04-27 116 views

回答

4

我的这种方法是简单地添加一个扩展方法,通过使用ConvertViewportPointToGeoCoordinate方法在控制返回边框地图控件:

public static class MapExtensions 
{ 
    /// <summary> 
    /// Returns the bounding rectangle representing the area displayed by the map contorl. 
    /// </summary> 
    /// <param name="map">The map control whose bounding rectangle should be returned.</param> 
    /// <returns>The bounding rectangle representing the area displayed by the map contorl.</returns> 
    public static MapBoundingRectangle GetBoundingRectangle(this Microsoft.Phone.Maps.Controls.Map map) 
    { 
     var lowerLeft = map.ConvertViewportPointToGeoCoordinate(new System.Windows.Point(0, map.ActualHeight)); 
     var upperRight = map.ConvertViewportPointToGeoCoordinate(new System.Windows.Point(map.ActualWidth, 0)); 

     return new MapBoundingRectangle(lowerLeft, upperRight); 
    } 
} 

哪里MapBoundingRectangle仅仅是一个简单的辅助类:

public class MapBoundingRectangle 
{ 
    #region -- Class constants 
    /// <summary> 
    /// The minimum value for a longitude (East-West position). 
    /// </summary> 
    public const double MinimumLongitude = -180; 

    /// <summary> 
    /// The maximum value for a longitude (East-West position). 
    /// </summary> 
    public const double MaximumLongitude = 180; 

    /// <summary> 
    /// The minimum value for a latitude (North-South position). 
    /// </summary> 
    public const double MinimumLatitude = -90; 

    /// <summary> 
    /// The maximum value for a latitude (North-South position). 
    /// </summary> 
    public const double MaximumLatitude = 90; 
    #endregion 

    #region -- Constructors 
    /// <summary> 
    /// Creates a bounding rectangle using the provided coordinates. 
    /// </summary> 
    /// <param name="lowerLeftLatitude">The latitude of the lower-left corner of the bounding rectangle.</param> 
    /// <param name="lowerLeftLongitude">The longitude of the lower-left corner of the bounding rectangle.</param> 
    /// <param name="upperRightLatitude">The latitude of the upper-right corner of the bounding rectangle.</param> 
    /// <param name="upperRightLongitude">The longitude of the upper-right corner of the bounding rectangle.</param> 
    /// <exception cref="ArgumentException"> 
    /// Thrown if any of the provided coordinates contains an invalid value or if the two corner coordinates 
    /// don't define a rectangle (i.e. if the lower-left and upper-right coordinates are swapped). 
    /// </exception> 
    public MapBoundingRectangle(double lowerLeftLatitude, double lowerLeftLongitude, double upperRightLatitude, double upperRightLongitude) 
    { 
     if (lowerLeftLatitude < MinimumLatitude) 
     { 
      throw new ArgumentException(Resources.Resources.MapBoundingRectangle_LatitudeTooSmall, "lowerLeftLatitude"); 
     } 

     if (lowerLeftLatitude > MaximumLatitude) 
     { 
      throw new ArgumentException(Resources.Resources.MapBoundingRectangle_LatitudeTooBig, "lowerLeftLatitude"); 
     } 

     if (upperRightLatitude < MinimumLatitude) 
     { 
      throw new ArgumentException(Resources.Resources.MapBoundingRectangle_LatitudeTooSmall, "upperRightLatitude"); 
     } 

     if (upperRightLatitude > MaximumLatitude) 
     { 
      throw new ArgumentException(Resources.Resources.MapBoundingRectangle_LatitudeTooBig, "upperRightLatitude"); 
     } 

     if (lowerLeftLongitude < MinimumLongitude) 
     { 
      throw new ArgumentException(Resources.Resources.MapBoundingRectangle_LatitudeTooSmall, "lowerLeftLongitude"); 
     } 

     if (lowerLeftLongitude > MaximumLongitude) 
     { 
      throw new ArgumentException(Resources.Resources.MapBoundingRectangle_LatitudeTooBig, "lowerLeftLongitude"); 
     } 

     if (upperRightLongitude < MinimumLongitude) 
     { 
      throw new ArgumentException(Resources.Resources.MapBoundingRectangle_LatitudeTooSmall, "upperRightLongitude"); 
     } 

     if (upperRightLongitude > MaximumLongitude) 
     { 
      throw new ArgumentException(Resources.Resources.MapBoundingRectangle_LatitudeTooBig, "upperRightLongitude"); 
     } 

     if (lowerLeftLatitude > upperRightLatitude || lowerLeftLongitude > upperRightLongitude) 
     { 
      throw new ArgumentException(Resources.Resources.MapBoundingRectangle_NotARectangle); 
     } 

     LowerLeft = new GeoCoordinate(lowerLeftLatitude, lowerLeftLongitude); 
     UpperRight = new GeoCoordinate(upperRightLatitude, upperRightLongitude); 
    } 

    /// <summary> 
    /// Creates a bounding rectangle using the provided coordinates. 
    /// </summary> 
    /// <param name="lowerLeft">The lower-left (south-west) corner of the bounding rectangle.</param> 
    /// <param name="upperRight">The upper-right (north-east) corner of the bounding rectangle.</param> 
    /// <exception cref="ArgumentException"> 
    /// Thrown if the two corner coordinates don't define a rectangle (i.e. if the lower-left and upper-right coordinates are swapped). 
    /// </exception> 
    /// <exception cref="ArgumentNullException"> 
    /// Thrown if either <paramref name="lowerLeft"/> or <paramref name="upperRight"/> is <c>null</c> 
    /// or <see cref="GeoCoordinate.Unknown"/>. 
    /// </exception> 
    public MapBoundingRectangle(GeoCoordinate lowerLeft, GeoCoordinate upperRight) 
    { 
     if (lowerLeft == null || lowerLeft == GeoCoordinate.Unknown) 
     { 
      throw new ArgumentNullException("lowerLeft"); 
     } 

     if (upperRight == null || upperRight == GeoCoordinate.Unknown) 
     { 
      throw new ArgumentNullException("upperRight"); 
     } 

     if (lowerLeft.Latitude > upperRight.Latitude || lowerLeft.Longitude > upperRight.Longitude) 
     { 
      throw new ArgumentException(Resources.Resources.MapBoundingRectangle_NotARectangle); 
     } 

     LowerLeft = lowerLeft; 
     UpperRight = upperRight; 
    } 
    #endregion 

    #region -- Public properties 
    /// <summary> 
    /// The lower left (south-west) corner of the bounding rectangle. 
    /// </summary> 
    public GeoCoordinate LowerLeft { get; private set; } 

    /// <summary> 
    /// The upper right (north-east) corner of the bounding rectangle. 
    /// </summary> 
    public GeoCoordinate UpperRight { get; private set; } 
    #endregion 

    #region -- Public methods 
    /// <summary> 
    /// Determines if this bounding rectangle completely contains 
    /// <paramref name="otherRectangle"/>. 
    /// </summary> 
    /// <param name="otherRectangle">The other <see cref="MapBoundingRectangle"/> to check.</param> 
    /// <returns> 
    /// <c>true</c> if <paramref name="otherRectangle"/> is completely within this bounding 
    /// rectangle, <c>false</c> otherwise. 
    /// </returns> 
    /// <exception cref="ArgumentNullException">Thrown if <paramref name="otherRectangle"/> is <c>null</c>.</exception> 
    public bool Contains(MapBoundingRectangle otherRectangle) 
    { 
     if (otherRectangle == null) 
     { 
      throw new ArgumentNullException("otherRectangle"); 
     } 

     bool lowerLeftContained  = (otherRectangle.LowerLeft.Latitude >= LowerLeft.Latitude && otherRectangle.LowerLeft.Longitude >= LowerLeft.Longitude); 
     bool upperRightContained = (otherRectangle.UpperRight.Latitude <= UpperRight.Latitude && otherRectangle.UpperRight.Longitude <= UpperRight.Longitude); 

     return (lowerLeftContained && upperRightContained); 
    } 

    /// <summary> 
    /// Returns the width (in meters) of the bounding rectangle. 
    /// </summary> 
    /// <returns>The width (in meters) of the bounding rectangle.</returns> 
    public double GetWidth() 
    { 
     GeoCoordinate leftEdge = LowerLeft; 
     GeoCoordinate rightEdge = new GeoCoordinate(LowerLeft.Latitude, UpperRight.Longitude); 

     return Math.Abs(leftEdge.GetDistanceTo(rightEdge)); 
    } 

    /// <summary> 
    /// Returns the height (in meters) of the bounding rectangle. 
    /// </summary> 
    /// <returns>The height (in meters) of the bounding rectangle.</returns> 
    public double GetHeight() 
    { 
     GeoCoordinate bottomEdge = LowerLeft; 
     GeoCoordinate upperEdge  = new GeoCoordinate(UpperRight.Latitude, LowerLeft.Longitude); 

     return Math.Abs(bottomEdge.GetDistanceTo(upperEdge)); 
    } 
    #endregion 
} 

可能不是最棒的解决方案,但它解决了我的需求,所以我希望它有所帮助。

+0

谢谢垫!工作正常! – Alexandr 2013-04-30 05:49:40

+0

它可能会提供错误的结果,当地图已经在ConvertViewportPointToGeoCoordinate之间移动了几个角落 – 2014-07-02 09:46:30

0

尝试

GeoCoordinate topLeft = Map.ConvertViewportPointToGeoCoordinate(new Point(0, 0)); 
      GeoCoordinate bottomRight = Map.ConvertViewportPointToGeoCoordinate(new Point(e.NewSize.Width, e.NewSize.Height)); 
      var bounds = LocationRectangle.CreateBoundingRectangle(topLeft, bottomRight); 

见LocationRectangle documenation