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

GetItemsInLocation

3 Answers 56 Views
Map
This is a migrated thread and some comments may be shown as answers.
Rieni De Rijke
Top achievements
Rank 1
Rieni De Rijke asked on 25 Aug 2011, 08:16 AM

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

3 Answers, 1 is accepted

Sort by
0
Andrey
Telerik team
answered on 30 Aug 2011, 01:10 PM
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 >>

0
Rieni De Rijke
Top achievements
Rank 1
answered on 28 Sep 2011, 08:33 AM

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

 

 

}

0
Accepted
Andrey
Telerik team
answered on 30 Sep 2011, 01:54 PM
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 >>

Tags
Map
Asked by
Rieni De Rijke
Top achievements
Rank 1
Answers by
Andrey
Telerik team
Rieni De Rijke
Top achievements
Rank 1
Share this question
or