Updating POI's in map best view

5 posts, 0 answers
  1. Michael
    Michael avatar
    11 posts
    Member since:
    May 2013

    Posted 28 Jun 2013 Link to this post

    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?
  2. Andrey
    Admin
    Andrey avatar
    1681 posts

    Posted 02 Jul 2013 Link to this post

    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 >>
  3. UI for WPF is Visual Studio 2017 Ready
  4. Michael
    Michael avatar
    11 posts
    Member since:
    May 2013

    Posted 02 Jul 2013 Link to this post

    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;
     
    }


  5. Andrey
    Admin
    Andrey avatar
    1681 posts

    Posted 03 Jul 2013 Link to this post

    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 >>
  6. Michael
    Michael avatar
    11 posts
    Member since:
    May 2013

    Posted 03 Jul 2013 Link to this post

    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.
Back to Top
UI for WPF is Visual Studio 2017 Ready