GetItemsInLocation

4 posts, 1 answers
  1. Rieni De Rijke
    Rieni De Rijke avatar
    170 posts
    Member since:
    Mar 2010

    Posted 25 Aug 2011 Link to this post

    I have a problem using InformationLayer.GetItemsInLocation(Location);

    My InformationLayer is binded to an ObservableCollection<MyClass> myObjektCollection

    <Map:InformationLayer Name="myObjektLayer" ItemsSource="{Binding myObjektCollection }" ItemTemplate="{StaticResource informationLayerFlyttObjektTemplate}" Visibility="Visible" />

    While adding MyClass items to myObjektCollection , I can see that both InformationLayer(myObjektLayer) and myObjektCollection increases.

    If I ,just after adding a new MyClass item to myObjektCollection, try to call

    InformationLayer(myObjektLayer).GetItemsInLocation(myLocation); returns no items found.

    If I later, after refreshing the map call the same method InformationLayer(myObjektLayer).GetItemsInLocation(myLocation); returns with expected items.

    I know that myLocation is valid.

    Ex.

    myObjektItemCollection.Add(myClass);

    InformationLayer(myObjektLayer).GetItemsInLocation(myLocation) -  No result

    In method MapMoved()

    InformationLayer(myObjektLayer).GetItemsInLocation(myLocation) -  OK result

    Is there any method I should call on InformationLayer or Observablecollection before I call InformationLayer(myObjektLayer).GetItemsInLocation(myLocation)?

    I tried:

    InformationLayer(myObjektLayer).BeginInit();

    myObjektItemCollection.Add(myClass);

    InformationLayer(myObjektLayer).EndInit();

    InformationLayer(myObjektLayer).GetItemsInLocation(myLocation)? No result

  2. Andrey
    Admin
    Andrey avatar
    1681 posts

    Posted 30 Aug 2011 Link to this post

    Hello Rieni,

    You should call the InformationLayer.GetItemsInLocation after the item is rendered when you have added it. So, I would recommend to call it in a method which is invoked using the Dispatcher.BeginInvoke method. Also I should note that the location which is used as a parameter of InformationLayer.GetItemsInLocation should be within the visible area of the map.
    The sample code is below:
    <Window x:Class="GetItemsInLocationTest.MainWindow"
            Title="MainWindow" Height="350" Width="525">
        <Grid>
            <telerik:RadMap x:Name="radMap"
                            ZoomLevel="5"
                            Center="40, -100">
                <telerik:RadMap.Provider>
                    <telerik:OpenStreetMapProvider />
                </telerik:RadMap.Provider>
                <telerik:InformationLayer x:Name="informationLayer">
                    <telerik:InformationLayer.ItemTemplate>
                        <DataTemplate>
                            <Border telerik:MapLayer.Location="{Binding Location}">
                                <telerik:MapLayer.HotSpot>
                                    <telerik:HotSpot X="0.5" Y="0.5" XUnits="Fraction" YUnits="Fraction" ElementName="PART_Image" />
                                </telerik:MapLayer.HotSpot>
                                <Grid>
                                    <Path Grid.Row="0" Fill="Brown" x:Name="PART_Image">
                                        <Path.Data>
                                            <GeometryGroup>
                                                <EllipseGeometry Center="7,7" RadiusX="7" RadiusY="7" />
                                            </GeometryGroup>
                                        </Path.Data>
                                    </Path>
                                </Grid>
                            </Border>
                        </DataTemplate>
                    </telerik:InformationLayer.ItemTemplate>
                </telerik:InformationLayer>
            </telerik:RadMap>
        </Grid>
    </Window>

    using System;
    using System.Collections.ObjectModel;
    using System.Windows;
    using Telerik.Windows.Controls.Map;
     
    namespace GetItemsInLocationTest
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            private ObservableCollection<MyClass> items = new ObservableCollection<MyClass>();
     
            public MainWindow()
            {
                InitializeComponent();
     
                this.radMap.InitializeCompleted += new EventHandler(radMap_InitializeCompleted);
            }
     
            private void radMap_InitializeCompleted(object sender, EventArgs e)
            {
                this.informationLayer.ItemsSource = items;
     
                items.Add(new MyClass()
                {
                    Location = new Location(40, -100)
                });
     
                this.Dispatcher.BeginInvoke(new Action(this.GetItemsInLocation));
            }
     
            private void GetItemsInLocation()
            {
                var location = new Location(40, -100);
                var itemsInLocation = this.informationLayer.GetItemsInLocation(location);
                foreach (var item in itemsInLocation)
                {
                    var myClass = item as MyClass;
                    if (myClass != null)
                    {
                        // the item is found
                        MessageBox.Show(item.ToString());
                    }
                }
            }
     
            public class MyClass
            {
                public Location Location
                {
                    get;
                    set;
                }
            }
        }
    }

    All the best,
    Andrey Murzov
    the Telerik team

    Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

  3. UI for WPF is Visual Studio 2017 Ready
  4. Rieni De Rijke
    Rieni De Rijke avatar
    170 posts
    Member since:
    Mar 2010

    Posted 28 Sep 2011 Link to this post

    If I do exactly like your ex. GetItemsInLocation works fine, and I get the MessageBox.
    But:
    If I implement the same code in PreviewMouseLeftButtonDown or in MouseLeftButtonDown, it does not work anymore.

    this.radMap.PreviewMouseLeftButtonDown += new MouseButtonEventHandler(radMap_PreviewMouseLeftButtonDown);


    void
    radMap_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)

    {

         this.informationLayer.ItemsSource = items;

     

         items.Add(new MyClass()

                         {

                             Location = new Location(40, -100)

                         });

     

          var location = new Location(40, -100);

          var itemsInLocation = this.informationLayer.GetItemsInLocation(location);

     

          this.Dispatcher.BeginInvoke(new Action(this.GetItemsInLocation));

     

     

    }

  5. Answer
    Andrey
    Admin
    Andrey avatar
    1681 posts

    Posted 30 Sep 2011 Link to this post

    Hi Rieni,

    I think in this case you can use the Dispatcher.Invoke method which allows to specify the priority for invoking method. I have used the DispatcherPriority.Render value. The added item has been found successfully.
    The sample code is below.
    private void radMap_PreviewMouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
    {
        this.informationLayer.ItemsSource = items;
     
        var newItem = new MyClass()
        {
            Location = new Location(40, -100)
        };
        items.Add(newItem);
     
        var location = new Location(40, -100);
     
        this.Dispatcher.Invoke(DispatcherPriority.Render, new Action<Location>(this.GetItemsInLocation), location);
    }
     
    private void GetItemsInLocation(Location location)
    {
        var itemsInLocation = this.informationLayer.GetItemsInLocation(location);
        foreach (var item in itemsInLocation)
        {
            var myClass = item as MyClass;
            if (myClass != null)
            {
                // the item is found
                MessageBox.Show(item.ToString());
            }
        }
    }

    Best wishes,
    Andrey Murzov
    the Telerik team

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

Back to Top