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

Bestview

1 Answer 39 Views
Map
This is a migrated thread and some comments may be shown as answers.
Joe Bohen
Top achievements
Rank 1
Joe Bohen asked on 15 Oct 2012, 10:03 AM

Hi,

I am attempting to set the best view on radmap but I am getting a type conversion error when setting the rectangle to the information layer.

I am setting the information layer item source to records within a radgrid using:

Me.informationLayer.ItemsSource = Nothing

            items = New ObservableCollection(Of MapItem)

            Dim Item As PendingClass = TryCast(Me.PendingGrid.SelectedItem, PendingClass)

            Dim LatLon() As String

            If Not Item.LatLon Is Nothing Then

                LatLon = Item.LatLon.Split(",")

                Me.Vehtn.Visibility = Windows.Visibility.Visible

                Dim foreground As Brush = New SolidColorBrush(Colors.Yellow)

                Dim background As Brush = New SolidColorBrush(Colors.Red)

                Dim Vtext As New TextBlock() With { _

                .Text = Item.DelRegistration, _

                .Background = background, _

                .Foreground = foreground _

                }

                Me.items.Add(New MapItem(LatLon(0), LatLon(1), background, Vtext, Item.DateOfFix))

            End If

            Background = New SolidColorBrush(Colors.Green)

            Dim text As New TextBlock() With { _

                .Text = Item.DelName, _

                .Background = Background, _

                .Foreground = Foreground _

            }

            If Not Item.DelLatLon Is Nothing Then

                Me.Locbtn.Visibility = Windows.Visibility.Visible

                If Me.Vehtn.Visibility = Windows.Visibility.Visible Then Me.Distancebtn.Visibility = Windows.Visibility.Visible

                LatLon = Item.DelLatLon.Split(",")

                Me.items.Add(New MapItem(LatLon(0), LatLon(1), Background, text, Item.DelDate))

            End If

            Me.informationLayer.ItemsSource = items

I then attempt to set the map best view and I get the error:

+                      ex         {"Unable to cast object of type 'System.Windows.Controls.ItemCollection' to type 'System.Collections.Generic.IEnumerable`1[System.Object]'."}      System.Exception’

If Me.informationLayer.Items.Count > 0 Then

                Dim rect As LocationRect = Me.informationLayer.GetBestView(Me.GetIEnumerable(Me.informationLayer))

                rect.MapControl = Me.radMap

                Me.radMap.Center = rect.Center

                Me.radMap.ZoomLevel = rect.ZoomLevel

            End If

Regards,
Joe

 

 

 

1 Answer, 1 is accepted

Sort by
0
Andrey
Telerik team
answered on 18 Oct 2012, 06:31 AM
Hello Joe,

There is two problems I have found in the code snippet you sent.

1. Unfortunately you have not sent the source code of the GetIEnumerable method you use. It looks it returns the ItemCollection instead of the IEnumerable of Object. Or it tries just to cast the InformationLayer.Items property to the IEnumerable of Object type. You can use the method like the following to return the IEnumerable of Object as the items of information layer:
Private Function GetIEnumerable(informationLayer As InformationLayer) As IEnumerable(Of Object)
    Dim collection = New Collection(Of Object)
    For Each item As Object In Me.informationLayer.Items
        collection.Add(item)
    Next
    Return collection
End Function

2. GetBestView method requires that items will be on the screen already. Otherwise it will not work. Similar to any other ItemsControl the information layer takes some time to process all markers in the list and show them over the map. So if you try to use this method immediately when the collection is changed, then it will not work, because at this moment items aren't on the screen yet.

Also the InformationLayer.GetBestView method takes in account visible size of the markers (in pixels) and makes best possible assumption about its geographical size when calculating location rectangle. But if it is enough for you to ensure that all locations are on the screen (regardless whether actual markers are visible), then you can use simplified version to calculate location rectangle which will account the location of items only.

I have attached a sample solution which calculates the best view this way.
I hope it helps.

Greetings,
Andrey Murzov
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

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