This is a migrated thread and some comments may be shown as answers.

Automatically Center and Zoom Map

3 Answers 397 Views
Map
This is a migrated thread and some comments may be shown as answers.
Sean
Top achievements
Rank 1
Sean asked on 26 Sep 2011, 08:29 PM
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;
}

3 Answers, 1 is accepted

Sort by
0
Sean
Top achievements
Rank 1
answered on 27 Sep 2011, 07:36 PM
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.
0
Paul
Top achievements
Rank 1
answered on 24 Nov 2011, 04:13 AM
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?
0
Andrey
Telerik team
answered on 28 Nov 2011, 08:42 AM
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 >>

Tags
Map
Asked by
Sean
Top achievements
Rank 1
Answers by
Sean
Top achievements
Rank 1
Paul
Top achievements
Rank 1
Andrey
Telerik team
Share this question
or