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

Updating POI's in map best view

4 Answers 111 Views
Map
This is a migrated thread and some comments may be shown as answers.
Michael
Top achievements
Rank 1
Michael asked on 28 Jun 2013, 05:41 PM
I have a radmap in an application that displays a collection of POI's. Its put together using an information layer with a data template that defines what they will look like. Its all mvvm, so no code behind events at this point.

The problem that I am having is that the map needs to update - and this usually consists of a different set of points. I have already read and know how I can reload the center and the zoom in the map loaded event. The problem is that when the data is refreshed - the map is not reloaded - and I also do not have access to the RadMap control (found a solution where you can set the center and zoom if the RadMap is part of the event argument based on a code-behind loaded event).

Anways, what I am trying to find is a way to update the center and zoom on the map (using LocationRect) when the POI's change. And I need to accomplish this without using code-behind events. Is there another event that I can wire up to a command/event that will fire when the POI's are cleared and then re-rendered... maybe something in the InformationLayer?

4 Answers, 1 is accepted

Sort by
0
Andrey
Telerik team
answered on 02 Jul 2013, 06:54 AM
Hello Michael,

The InformationLayer (like any other ItemsControl) do not provide any events when source collection is changed. Instead you should use events provided by your View Model (this is a right way to do the things). Since you are talking about MVVM pattern then your model should implement INotifyPropertyChanged interface. Since you are binding the InformationLayer.ItemsSource property to the collection of POI then, most probably, this collection implements CollectionChanged event. You can use these events in your UI code to set zoom level and center of the RadMap.

Or you can create Center and ZoomLevel properties in your view model and bind RadMap properties to them. When collection of POIs in your model is changed you should recalculate correspondent properties in the model.

Regards,
Andrey Murzov
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WPF.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
0
Michael
Top achievements
Rank 1
answered on 02 Jul 2013, 02:06 PM
Hi Andrey,

Yes, thats currently what I'm doing. I have the Center and ZoomLevel bound to the ViewModel. Also, I re-calculate the LocationRect (in the ViewModel) when the POI's change and update in the ViewModel. The problem is that the ZoomLevel always comes back as 1. I found a solution to this in the Telerik forums. The problem is with the BestView. I build the location rect, get the correct center - but as I stated before the zoom comes back as 1. This is resolved at the bottom where the BestView.MapControl is assigned to the radmap. From there you can get the correct zoom. Ideally this is the solution I would like to use.

The method below comes from the ViewModel (I know that tehcnically referencing the Radmap is bad practice as its a UI element). The only way I can get the command event to fire (and thus get the source in this case the radmap) is the Loaded event in the map. 

So my problem is that I need that correct zoom level. The only way to get it is when the RadMap is passed as the event arg. This occurs only in the Loaded event. When I re-render POI's no loaded event is called and hence - I cannot use the code below to setup the zoom and center.

    RadMap radMap = (RadMap)e.Source; //from routedeventargs
     
    BestView = new LocationRect();
    Location northEast = Location.Empty;
    Location southWest = Location.Empty;
 
    Size defaultSize = new Size(0.001, 0.001);
 
    foreach (Location location in this.rectList)
    {
        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));
            }
        }
    }           
 
    BestView.MapControl = radMap;
    Center = BestView.ViewCenter;
    ZoomLevel = BestView.ZoomLevel;
 
}


0
Andrey
Telerik team
answered on 03 Jul 2013, 08:01 AM
Hello Michael,

You shouldn't use UI events when collection of POIs changed. You should use events generated by your ViewModel. Please, find attached, the sample solution which demonstrates how the event generated in ViewModel can be used to set best view.

Regards,
Andrey Murzov
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WPF.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
0
Michael
Top achievements
Rank 1
answered on 03 Jul 2013, 02:32 PM
I guess I should have pasted more code. This is a method inside of my ViewModel. Anyways, let me try out the example you sent me. Seems like the answer I was looking for.
Tags
Map
Asked by
Michael
Top achievements
Rank 1
Answers by
Andrey
Telerik team
Michael
Top achievements
Rank 1
Share this question
or