This project has moved. For the latest updates, please go here.

"BoundingRectangle" equivelent

Apr 8, 2014 at 12:45 PM
The Bing Maps control for WPF that I am moving away from due to performance issues exposed a "BoundingRectangle" property that basically exposed the Lat/Lng coordinates of the North West and South East corners of the visible map.

I am trying to recreate this on your map control as I use it to filter the number of MapItems that I display, which in turn improves performance. Given that I do not use/require any kind of rotation of the map, I have come up with the following attached behaviour that will allow me to bind to the current map "viewport"...
    class MapWindowViewportBehavior : Behavior<Map> {

        public static DependencyProperty BoundingRectangleProperty = DependencyProperty.RegisterAttached("BoundingRectangle",
            typeof(BoundingRectangle), typeof(MapWindowViewportBehavior), new FrameworkPropertyMetadata(null));

        public BoundingRectangle BoundingRectangle {
            get { return (BoundingRectangle)this.GetValue(MapWindowViewportBehavior.BoundingRectangleProperty); }
            set { this.SetValue(MapWindowViewportBehavior.BoundingRectangleProperty, value); }
        }

        protected override void OnAttached() {
            base.OnAttached();

            this.AssociatedObject.ViewportChanged += this.OnViewportChanged;
        }

        protected override void OnDetaching() {

            if (this.AssociatedObject != null) {
                this.AssociatedObject.ViewportChanged -= this.OnViewportChanged;
            }

            base.OnDetaching();
        }

        private void OnViewportChanged(object sender, EventArgs e) {
            this.BoundingRectangle = new BoundingRectangle(
                this.AssociatedObject.ViewportPointToLocation(new Point(0, 0)),
                this.AssociatedObject.ViewportPointToLocation(new Point(
                    this.AssociatedObject.ActualWidth,
                    this.AssociatedObject.ActualHeight
                )));
        }

    }

    class BoundingRectangle {

        public BoundingRectangle(Location northWest, Location southEast) {
            this.NorthWest = northWest;
            this.SouthEast = southEast;
        }

        public Location NorthWest { get; private set; }

        public Location SouthEast { get; private set; }

        public Boolean Contains(Double latitude, Double longitude) {
            if (this.NorthWest.Longitude > this.SouthEast.Longitude) {
                return longitude <= this.NorthWest.Latitude && this.SouthEast.Latitude <= longitude
                    && latitude <= this.NorthWest.Longitude && this.SouthEast.Longitude <= latitude;
            }
            else {
                return this.NorthWest.Longitude <= longitude && longitude <= this.SouthEast.Longitude
                    && latitude <= this.NorthWest.Latitude && this.SouthEast.Latitude <= latitude;
            }
        }

        public Boolean Contains(Location location) {
            return this.Contains(location.Latitude, location.Longitude);
        }

    }
Have I missed something within the control that already does this for me?
Is there a better way to do this?

Martin.
Coordinator
Apr 8, 2014 at 2:09 PM
Looks ok to me, except perhaps that you would usually declare a dependency property as public static readonly and you could omit the forth argument to Register (the property metadata), but that is of course nit-picking.

And no, you haven't missed anything. There is no such property in the map control.