Automatically Center and Zoom Map

4 posts, 0 answers
  1. Sean
    Sean avatar
    10 posts
    Member since:
    Aug 2011

    Posted 26 Sep 2011 Link to this post

    I'm using RadMap in an MVVM pattern, so I'm not able to get a reference to the map or the InformationLayer so that I can get the best possible LocationRect based on information in the view as mentioned in the first response in this thead:

    http://www.telerik.com/community/forums/wpf/map/how-to-get-best-possible-map-view-when-considering-all-poi-s.aspx

    I attempted to use the second approach (the method posted at the end of the post), but it too is giving me some problems. I'm able to get a LocationRect, but I'm not able to call the SetView method to center/zoom the map appropriately.

    I created properties in my view model for center and zoom. The MapCenter property seems to be working fine, but I'm having a hard time with the zoom property.

    List<Telerik.Windows.Controls.Map.Location> locations = this.MyPushpins.Select(pin => pin.Location).ToList();
    LocationRect rect = this.GetBestView(locations, new Size(0.01, 0.01));
     
    MapCenter = rect.Center;
    MapZoomLevel = rect.ZoomLevel;

    The problem is the LocationRect.ZoomLevel property isn't set and I don't know how to set it without having more information from the map, itself.

    For reference, here is the method posted in the original thread:

    public LocationRect GetBestView(IEnumerable<PointOfInterest> itemsList, Size defaultSize)
    {
        LocationRect bestView = new LocationRect();
        Location northEast = Location.Empty;
        Location southWest = Location.Empty;
       
        foreach (PointOfInterest item in itemsList)
        {
            Location location = item.Location;
       
            if (northEast.IsEmpty)
            {
                northEast = location;
            }
            else
            {
                if (!location.IsEmpty)
                {
                    northEast.Latitude = Math.Max(northEast.Latitude, location.Latitude);
                    northEast.Longitude = Math.Max(northEast.Longitude, location.Longitude);
                }
            }
       
            if (southWest.IsEmpty)
            {
                southWest = location;
            }
            else
            {
                if (!location.IsEmpty)
                {
                    southWest.Latitude = Math.Min(southWest.Latitude, location.Latitude);
                    southWest.Longitude = Math.Min(southWest.Longitude, location.Longitude);
                }
            }
        }
       
        if (!northEast.IsEmpty && !southWest.IsEmpty)
        {
            bestView = new LocationRect(northEast, southWest);
       
            if (bestView.IsEmpty)
            {
                bestView = new LocationRect(
                    new Location(bestView.North + defaultSize.Height / 2.0, bestView.West - defaultSize.Width / 2.0),
                    new Location(bestView.North - defaultSize.Height / 2.0, bestView.West + defaultSize.Width / 2.0));
            }
        }
       
        return bestView;
    }
  2. Sean
    Sean avatar
    10 posts
    Member since:
    Aug 2011

    Posted 27 Sep 2011 Link to this post

    I figured it out by using JustDecompile to view the SetView method and ZoomLevel property of the LocactionRect class. I just needed to provide a reference to the map to the LocationRect object and the ZoomLevel property is property calculated.
  3. UI for WPF is Visual Studio 2017 Ready
  4. Paul
    Paul avatar
    2 posts
    Member since:
    Aug 2011

    Posted 23 Nov 2011 Link to this post

    I am also trying to do this through MVVM.

    I can't have a reference to the MapControl in the ViewModel so I assume there is no way to do this. Seems you would need to know the size on the MapControl window to be able to calculate the ZoomLevel required.

    Any advice on how to accomplish this in an MVVM way?
  5. Andrey
    Admin
    Andrey avatar
    1681 posts

    Posted 28 Nov 2011 Link to this post

    Hello,

    The map control is required for calculating the zoom level of best view according to its projection. In your view model you can use an event which raises when the best view is changed. I have attached a sample solution which invokes RadMap.SetView when this event occurs.

    All the best,
    Andrey Murzov
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

Back to Top