诺基亚地图Windows Phone中的BoundingRectangle

时间:2013-04-27 07:29:58

标签: windows-phone-7 windows-phone-8 here-api

如何获取访问地图边界矩形?

在WP7 Bing Map中,我可以使用属性BoundingRectangle,但在新的诺基亚地图中,我看不到类似的属性。

感谢提前。

2 个答案:

答案 0 :(得分:4)

我的方法是简单地为Map控件添加一个扩展方法,该方法使用控件上的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
}

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

答案 1 :(得分: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);

请参阅位置矩形文档