Mini Kabibi Habibi
Imports Microsoft.VisualBasic
Imports System.Collections.Generic
Imports System.Collections.ObjectModel
Imports System.ComponentModel
Imports System.Windows.Media.Effects
Imports DevExpress.Xpf.Map
Imports System.Windows.Data
Imports System
Imports System.Globalization
Imports System.Windows
Namespace MapDemo
Public Enum RouteModelState
Normal
Drive
End Enum
Public Class NavigatorDataModel
Inherits FrameworkElement
Implements INotifyPropertyChanged
Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
Protected Sub NotifyPropertyChanged(ByVal propertyName As String)
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
End Sub
Protected Sub NotifyPropertyChanged(ByVal sender As Object, ByVal e As PropertyChangedEventArgs)
RaiseEvent PropertyChanged(sender, e)
End Sub
End Class
Public Class RouteModel
Inherits NavigatorDataModel
Public Shared ReadOnly MapCenterProperty As DependencyProperty = DependencyProperty.Register("MapCenter", GetType(GeoPoint), GetType(RouteModel), New PropertyMetadata(New GeoPoint(34.158506,-118.255629)))
Public Shared ReadOnly TimeScaleProperty As DependencyProperty = DependencyProperty.Register("TimeScale", GetType(Double), GetType(RouteModel), New PropertyMetadata(1.0))
Private navigator_Renamed As Navigator
Private helpers_Renamed As ObservableCollection(Of MapItem)
Private isCalculating_Renamed As Boolean
Private state_Renamed As RouteModelState
Private driveModel_Renamed As DriveModel
Private waypoints_Renamed As List(Of RouteWaypoint)
Private routePushpins_Renamed As List(Of MapPushpin)
Private routePath_Renamed As List(Of GeoPoint)
Private itineraryItems_Renamed As List(Of BingItineraryItem)
Private waypointIndex As Integer
Private searchPushpinsCount As Integer
Private drivePathDistance_Renamed As Double
Public ReadOnly Property Helpers() As ObservableCollection(Of MapItem)
Get
Return Me.helpers_Renamed
End Get
End Property
Public Property IsCalculating() As Boolean
Get
Return Me.isCalculating_Renamed
End Get
Set(ByVal value As Boolean)
If Me.isCalculating_Renamed <> value Then
Me.isCalculating_Renamed = value
NotifyPropertyChanged("IsCalculating")
End If
End Set
End Property
Public Property State() As RouteModelState
Get
Return Me.state_Renamed
End Get
Set(ByVal value As RouteModelState)
If Me.state_Renamed <> value Then
Me.state_Renamed = value
OnStateChanged()
NotifyPropertyChanged("State")
End If
End Set
End Property
Public Property DriveModel() As DriveModel
Get
Return driveModel_Renamed
End Get
Set(ByVal value As DriveModel)
If Me.driveModel_Renamed IsNot value Then
Me.driveModel_Renamed = value
NotifyPropertyChanged("DriveModel")
End If
End Set
End Property
Public Property Navigator() As Navigator
Get
Return Me.navigator_Renamed
End Get
Set(ByVal value As Navigator)
If Me.navigator_Renamed IsNot value Then
Me.navigator_Renamed = value
NotifyPropertyChanged("Controller")
End If
End Set
End Property
Public ReadOnly Property RoutePath() As List(Of GeoPoint)
Get
Return routePath_Renamed
End Get
End Property
Public Property DrivePathDistance() As Double
Get
Return Me.drivePathDistance_Renamed
End Get
Set(ByVal value As Double)
Me.drivePathDistance_Renamed = value
End Set
End Property
Public Property ItineraryItems() As List(Of BingItineraryItem)
Get
Return Me.itineraryItems_Renamed
End Get
Set(ByVal value As List(Of BingItineraryItem))
Me.itineraryItems_Renamed = value
End Set
End Property
Public Property RoutePushpins() As List(Of MapPushpin)
Get
Return Me.routePushpins_Renamed
End Get
Set(ByVal value As List(Of MapPushpin))
Me.routePushpins_Renamed = value
End Set
End Property
Public ReadOnly Property ActionText() As String
Get
If (state_Renamed = RouteModelState.Drive) AndAlso (driveModel_Renamed IsNot Nothing) Then
Return driveModel_Renamed.ActionText
Else
If waypoints_Renamed.Count = 0 Then
If searchPushpinsCount > 0 Then
Return "Click the pushpin to set a start point."
Else
Return "Click the map or use Search to find a location."
End If
ElseIf waypoints_Renamed.Count = 1 Then
Return "Set a finish point to calculate a route."
Else
Return "Set another finish point or click Drive."
End If
End If
End Get
End Property
Public Property MapCenter() As GeoPoint
Get
Return CType(GetValue(MapCenterProperty), GeoPoint)
End Get
Set(ByVal value As GeoPoint)
SetValue(MapCenterProperty, value)
End Set
End Property
Public Property TimeScale() As Double
Get
Return CDbl(GetValue(TimeScaleProperty))
End Get
Set(ByVal value As Double)
SetValue(TimeScaleProperty, value)
End Set
End Property
Public ReadOnly Property Waypoints() As List(Of RouteWaypoint)
Get
Return Me.waypoints_Renamed
End Get
End Property
Public Sub New(ByVal navigator As Navigator)
Me.navigator_Renamed = navigator
Me.helpers_Renamed = New ObservableCollection(Of MapItem)()
Me.waypoints_Renamed = New List(Of RouteWaypoint)()
Me.routePushpins_Renamed = New List(Of MapPushpin)()
Me.itineraryItems_Renamed = New List(Of BingItineraryItem)()
End Sub
Private Sub SendRouteRequest()
IsCalculating = True
If waypoints_Renamed.Count > 1 Then
Navigator.RouteProvider.CalculateRoute(waypoints_Renamed)
End If
End Sub
Private Function NextWaypointLetter() As String
Dim letter As String = "" & ChrW(CByte(AscW("A"c)) + waypointIndex Mod 26)
waypointIndex += 1
Return letter
End Function
Private Sub ExtractItineraryItems(ByVal result As BingRouteResult)
itineraryItems_Renamed.Clear()
For Each leg As BingRouteLeg In result.Legs
For Each item As BingItineraryItem In leg.Itinerary
itineraryItems_Renamed.Add(item)
Next item
Next leg
End Sub
Private Sub BeginDrive()
If (routePath_Renamed IsNot Nothing) AndAlso (routePath_Renamed.Count > 1) Then
StopDrive()
DriveModel = New DriveModel(Me)
Dim mapCenterBinding As New Binding("MapCenterBinding") With {.Path = New PropertyPath("CurrentLocation"), .Source = DriveModel}
SetBinding(MapCenterProperty, mapCenterBinding)
End If
End Sub
Private Sub StopDrive()
If DriveModel IsNot Nothing Then
DriveModel.Cleanup()
DriveModel = Nothing
MapCenter = CType(MapCenter, GeoPoint)
End If
End Sub
Private Sub OnStateChanged()
Select Case State
Case RouteModelState.Drive
BeginDrive()
Case RouteModelState.Normal
StopDrive()
End Select
NotifyPropertyChanged("ActionText")
End Sub
Private Sub CalculatePathDistance()
drivePathDistance_Renamed = 0
If routePath_Renamed IsNot Nothing Then
Dim lastPoint? As MapUnit = Nothing
For Each node As GeoPoint In routePath_Renamed
If lastPoint.HasValue Then
Dim currentPoint As MapUnit = Navigator.RouteLayer.GeoPointToMapUnit(node)
drivePathDistance_Renamed += DriveModel.DistanceBetweenPoints(currentPoint, lastPoint.Value)
lastPoint = currentPoint
Else
lastPoint = Navigator.RouteLayer.GeoPointToMapUnit(node)
End If
Next node
End If
End Sub
Friend Sub NotifyDriveModelChanged()
NotifyPropertyChanged("ActionText")
End Sub
Friend Sub NotifySearchPushpinsChanged(ByVal count As Integer)
searchPushpinsCount = count
NotifyPropertyChanged("ActionText")
End Sub
Public Sub AddWaypoint(ByVal description As String, ByVal location As GeoPoint)
Dim waypoint As New RouteWaypoint(description, location)
If (Not waypoints_Renamed.Contains(waypoint)) Then
Dim pushpin As New MapPushpin()
pushpin.Location = location
pushpin.Information = description
pushpin.Text = NextWaypointLetter()
pushpin.TraceDepth = 0
pushpin.State = MapPushpinState.Busy
Helpers.Add(pushpin)
waypoints_Renamed.Add(waypoint)
SendRouteRequest()
End If
NotifyPropertyChanged("ActionText")
NotifyPropertyChanged("Waypoints")
End Sub
Public Sub ProcessRouteItems(ByVal items() As MapItem)
waypointIndex = 0
routePushpins_Renamed.Clear()
For Each item As MapItem In items
Dim pushpin As MapPushpin = TryCast(item, MapPushpin)
If pushpin IsNot Nothing Then
pushpin.Text = NextWaypointLetter()
routePushpins_Renamed.Add(pushpin)
End If
Dim polyline As MapPolyline = TryCast(item, MapPolyline)
If polyline IsNot Nothing Then
polyline.Effect = New DropShadowEffect() With {.Direction = -90, .ShadowDepth = 1}
End If
Next item
Helpers.Clear()
End Sub
Public Sub ProcessRouteResult(ByVal result As BingRouteResult)
If result IsNot Nothing Then
ExtractItineraryItems(result)
routePath_Renamed = result.RoutePath
Else
routePath_Renamed = Nothing
End If
CalculatePathDistance()
End Sub
Public Sub Clear()
If RoutePath IsNot Nothing Then
RoutePath.Clear()
End If
Helpers.Clear()
RoutePushpins.Clear()
ItineraryItems.Clear()
DrivePathDistance = 0.0
waypoints_Renamed.Clear()
waypointIndex = 0
NotifySearchPushpinsChanged(0)
NotifyPropertyChanged("Waypoints")
End Sub
End Class
Public Class RouteModelStateToButtonTextConverter
Implements IValueConverter
#Region "IValueConverter Members"
Public Function Convert(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As CultureInfo) As Object Implements IValueConverter.Convert
If (value IsNot Nothing) AndAlso (value.GetType() Is GetType(RouteModelState)) Then
Dim state As RouteModelState = CType(value, RouteModelState)
Select Case state
Case RouteModelState.Drive
Return "Stop"
Case RouteModelState.Normal
Return "Drive"
End Select
End If
Return String.Empty
End Function
Public Function ConvertBack(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As CultureInfo) As Object Implements IValueConverter.ConvertBack
Throw New NotImplementedException()
End Function
#End Region
End Class
Public Class RouteModelNormalStateToBoolConverter
Implements IValueConverter
#Region "IValueConverter Members"
Public Function Convert(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As CultureInfo) As Object Implements IValueConverter.Convert
If (value IsNot Nothing) AndAlso (value.GetType() Is GetType(RouteModelState)) Then
Return (CType(value, RouteModelState)) = RouteModelState.Normal
End If
Return False
End Function
Public Function ConvertBack(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As CultureInfo) As Object Implements IValueConverter.ConvertBack
Throw New NotImplementedException()
End Function
#End Region
End Class
Public Class WaypointsCountToDriveAbilityConverter
Implements IValueConverter
#Region "IValueConverter Members"
Public Function Convert(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As CultureInfo) As Object Implements IValueConverter.Convert
If (value IsNot Nothing) AndAlso (value.GetType() Is GetType(Integer)) Then
Return (CInt(Fix(value))) > 1
End If
Return False
End Function
Public Function ConvertBack(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As CultureInfo) As Object Implements IValueConverter.ConvertBack
Throw New NotImplementedException()
End Function
#End Region
End Class
End Namespace