// Here we update the vehicles from the database. In this sample we create our data provider each time and // when we are finsihed with it we dispose it. This is very safe however you may get better performance // if you were to cache this. We wanted to sample to be clean and safe and it is up to your expertise to // enhance it further private void UpdateVehicles(LayerOverlay traceOverlay, DateTime currentTime) { Dictionary <int, Vehicle> currentVehicles; TrackingAccessProvider vehicleProvider = new TrackingAccessProvider(dataRootPath); currentVehicles = vehicleProvider.GetCurrentVehicles(currentTime); // Loop through all the vehicle to add the history points if (currentVehicles != null && currentVehicles.All(v => !string.IsNullOrEmpty(v.Value.IconPath))) { InMemoryFeatureLayer vehicleTrailLayer; // Initialize vehicle overlay if it's not initialized if (!traceOverlay.Layers.Contains("VehicleTrail")) { // Create an InMemoryMarkerOverlay for the vehicle to hold the points and current location vehicleTrailLayer = new InMemoryFeatureLayer(); vehicleTrailLayer.Open(); vehicleTrailLayer.ZoomLevelSet.ZoomLevel01.DefaultPointStyle = GetVehicleTrailStyle(); vehicleTrailLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20; // Add all the required columns so we can populate later vehicleTrailLayer.FeatureSource.Open(); vehicleTrailLayer.Columns.Add(new FeatureSourceColumn("Speed")); vehicleTrailLayer.Columns.Add(new FeatureSourceColumn("DateTime")); vehicleTrailLayer.Columns.Add(new FeatureSourceColumn("Longitude")); vehicleTrailLayer.Columns.Add(new FeatureSourceColumn("Latitude")); vehicleTrailLayer.Columns.Add(new FeatureSourceColumn("VehicleName")); vehicleTrailLayer.Columns.Add(new FeatureSourceColumn("Duration")); Application.Current.Dispatcher.BeginInvoke(new Action(() => { lock (traceOverlay.Layers) { traceOverlay.Layers.Add("VehicleTrail", vehicleTrailLayer); } })); } else { // Find the overlay in the map vehicleTrailLayer = (InMemoryFeatureLayer)traceOverlay.Layers["VehicleTrail"]; } vehicleTrailLayer.InternalFeatures.Clear(); foreach (int vehicleId in currentVehicles.Keys) { Vehicle currentVehicle = currentVehicles[vehicleId]; VehicleViewModel tempVehicleViewModel = Vehicles.FirstOrDefault(v => v.OwnerName == currentVehicle.Name); if (tempVehicleViewModel == null) { tempVehicleViewModel = new VehicleViewModel(currentVehicle, mapModel); Application.Current.Dispatcher.BeginInvoke(new Action(() => Vehicles.Add(tempVehicleViewModel))); } else { tempVehicleViewModel.Vehicle = currentVehicle; tempVehicleViewModel.Load(); } bool isInSpatialFence = IsInSpatialFence(currentVehicle); tempVehicleViewModel.Area = isInSpatialFence ? "In restricted area" : "Out of restricted area"; // Add the vheicle's bread crumbs foreach (Location historyLocation in currentVehicle.HistoryLocations.Take(6)) { Feature breadcrumbFeature = new Feature(historyLocation.GetLocation().GetWellKnownBinary(), currentVehicle.Name + historyLocation.DateTime.ToString(CultureInfo.InvariantCulture)); breadcrumbFeature.ColumnValues["DateTime"] = historyLocation.DateTime.ToString(CultureInfo.InvariantCulture); breadcrumbFeature.ColumnValues["Speed"] = historyLocation.Speed.ToString(CultureInfo.InvariantCulture); breadcrumbFeature.ColumnValues["Longitude"] = historyLocation.Longitude.ToString(CultureInfo.InvariantCulture); breadcrumbFeature.ColumnValues["Latitude"] = historyLocation.Latitude.ToString(CultureInfo.InvariantCulture); breadcrumbFeature.ColumnValues["VehicleName"] = currentVehicle.Name; breadcrumbFeature.ColumnValues["Duration"] = currentVehicle.GetSpeedDuration().ToString(CultureInfo.InvariantCulture); vehicleTrailLayer.InternalFeatures.Add(breadcrumbFeature.Id, breadcrumbFeature); } InMemoryFeatureLayer currentVehicleLayer; if (!traceOverlay.Layers.Contains(currentVehicle.Name)) { // Create an InMemoryMarkerOverlay for the vehicle to hold the points and current location currentVehicleLayer = new InMemoryFeatureLayer(); currentVehicleLayer.Open(); currentVehicleLayer.Name = currentVehicle.Id.ToString(CultureInfo.InvariantCulture); currentVehicleLayer.ZoomLevelSet.ZoomLevel01.DefaultPointStyle = GetCurrentVehicleStyle(currentVehicle.IconPath); currentVehicleLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20; // Add all the required columns so we can populate later currentVehicleLayer.FeatureSource.Open(); currentVehicleLayer.Columns.Add(new FeatureSourceColumn("Speed")); currentVehicleLayer.Columns.Add(new FeatureSourceColumn("DateTime")); currentVehicleLayer.Columns.Add(new FeatureSourceColumn("Longitude")); currentVehicleLayer.Columns.Add(new FeatureSourceColumn("Latitude")); currentVehicleLayer.Columns.Add(new FeatureSourceColumn("VehicleName")); currentVehicleLayer.Columns.Add(new FeatureSourceColumn("Duration")); Application.Current.Dispatcher.BeginInvoke(new Action(() => { lock (traceOverlay.Layers) { traceOverlay.Layers.Add(currentVehicle.Name, currentVehicleLayer); } })); } else { // Find the overlay in the map currentVehicleLayer = (InMemoryFeatureLayer)traceOverlay.Layers[currentVehicle.Name]; } currentVehicleLayer.InternalFeatures.Clear(); // Add the vehicle's latest position Feature latestPositionFeature = new Feature(currentVehicle.Location.GetLocation().GetWellKnownBinary(), currentVehicle.Name); latestPositionFeature.ColumnValues["DateTime"] = currentVehicle.Location.DateTime.ToString(CultureInfo.InvariantCulture); latestPositionFeature.ColumnValues["Speed"] = currentVehicle.Location.Speed.ToString(CultureInfo.InvariantCulture); latestPositionFeature.ColumnValues["Longitude"] = currentVehicle.Location.Longitude.ToString(CultureInfo.InvariantCulture); latestPositionFeature.ColumnValues["Latitude"] = currentVehicle.Location.Latitude.ToString(CultureInfo.InvariantCulture); latestPositionFeature.ColumnValues["VehicleName"] = currentVehicle.Name; latestPositionFeature.ColumnValues["Duration"] = currentVehicle.GetSpeedDuration().ToString(CultureInfo.InvariantCulture); currentVehicleLayer.InternalFeatures.Add(latestPositionFeature.Id, latestPositionFeature); } } }
// Here we update the vehicles from the database. In this sample we create our data provider each time and // when we are finsihed with it we dispose it. This is very safe however you may get better performance // if you were to cache this. We wanted to sample to be clean and safe and it is up to your expertise to // enhance it further private void UpdateVehicles(LayerOverlay traceOverlay, DateTime currentTime) { Dictionary<int, Vehicle> currentVehicles; using (TrackingAccessProvider vehicleProvider = new TrackingAccessProvider(databasePathFileName)) { currentVehicles = vehicleProvider.GetCurrentVehicles(currentTime); } // Loop through all the vehicle to add the history points if (currentVehicles != null && currentVehicles.All(v => !string.IsNullOrEmpty(v.Value.IconPath))) { InMemoryFeatureLayer vehicleTrailLayer; // Initialize vehicle overlay if it's not initialized if (!traceOverlay.Layers.Contains("VehicleTrail")) { // Create an InMemoryMarkerOverlay for the vehicle to hold the points and current location vehicleTrailLayer = new InMemoryFeatureLayer(); vehicleTrailLayer.Open(); vehicleTrailLayer.ZoomLevelSet.ZoomLevel01.DefaultPointStyle = GetVehicleTrailStyle(); vehicleTrailLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20; // Add all the required columns so we can populate later vehicleTrailLayer.FeatureSource.Open(); vehicleTrailLayer.Columns.Add(new FeatureSourceColumn("Speed")); vehicleTrailLayer.Columns.Add(new FeatureSourceColumn("DateTime")); vehicleTrailLayer.Columns.Add(new FeatureSourceColumn("Longitude")); vehicleTrailLayer.Columns.Add(new FeatureSourceColumn("Latitude")); vehicleTrailLayer.Columns.Add(new FeatureSourceColumn("VehicleName")); vehicleTrailLayer.Columns.Add(new FeatureSourceColumn("Duration")); Application.Current.Dispatcher.BeginInvoke(new Action(() => { lock (traceOverlay.Layers) { traceOverlay.Layers.Add("VehicleTrail", vehicleTrailLayer); } })); } else { // Find the overlay in the map vehicleTrailLayer = (InMemoryFeatureLayer)traceOverlay.Layers["VehicleTrail"]; } vehicleTrailLayer.InternalFeatures.Clear(); foreach (int vehicleId in currentVehicles.Keys) { Vehicle currentVehicle = currentVehicles[vehicleId]; VehicleViewModel tempVehicleViewModel = Vehicles.FirstOrDefault(v => v.OwnerName == currentVehicle.Name); if (tempVehicleViewModel == null) { tempVehicleViewModel = new VehicleViewModel(currentVehicle, mapModel); Application.Current.Dispatcher.BeginInvoke(new Action(() => Vehicles.Add(tempVehicleViewModel))); } else { tempVehicleViewModel.Vehicle = currentVehicle; tempVehicleViewModel.Load(); } bool isInSpatialFence = IsInSpatialFence(currentVehicle); tempVehicleViewModel.Area = isInSpatialFence ? "In restricted area" : "Out of restricted area"; // Add the vheicle's bread crumbs foreach (Location historyLocation in currentVehicle.HistoryLocations.Take(6)) { Feature breadcrumbFeature = new Feature(historyLocation.GetLocation().GetWellKnownBinary(), currentVehicle.Name + historyLocation.DateTime.ToString(CultureInfo.InvariantCulture)); breadcrumbFeature.ColumnValues["DateTime"] = historyLocation.DateTime.ToString(CultureInfo.InvariantCulture); breadcrumbFeature.ColumnValues["Speed"] = historyLocation.Speed.ToString(CultureInfo.InvariantCulture); breadcrumbFeature.ColumnValues["Longitude"] = historyLocation.Longitude.ToString(CultureInfo.InvariantCulture); breadcrumbFeature.ColumnValues["Latitude"] = historyLocation.Latitude.ToString(CultureInfo.InvariantCulture); breadcrumbFeature.ColumnValues["VehicleName"] = currentVehicle.Name; breadcrumbFeature.ColumnValues["Duration"] = currentVehicle.GetSpeedDuration().ToString(CultureInfo.InvariantCulture); vehicleTrailLayer.InternalFeatures.Add(breadcrumbFeature.Id, breadcrumbFeature); } InMemoryFeatureLayer currentVehicleLayer; if (!traceOverlay.Layers.Contains(currentVehicle.Name)) { // Create an InMemoryMarkerOverlay for the vehicle to hold the points and current location currentVehicleLayer = new InMemoryFeatureLayer(); currentVehicleLayer.Open(); currentVehicleLayer.Name = currentVehicle.Id.ToString(CultureInfo.InvariantCulture); currentVehicleLayer.ZoomLevelSet.ZoomLevel01.DefaultPointStyle = GetCurrentVehicleStyle(currentVehicle.IconPath); currentVehicleLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20; // Add all the required columns so we can populate later currentVehicleLayer.FeatureSource.Open(); currentVehicleLayer.Columns.Add(new FeatureSourceColumn("Speed")); currentVehicleLayer.Columns.Add(new FeatureSourceColumn("DateTime")); currentVehicleLayer.Columns.Add(new FeatureSourceColumn("Longitude")); currentVehicleLayer.Columns.Add(new FeatureSourceColumn("Latitude")); currentVehicleLayer.Columns.Add(new FeatureSourceColumn("VehicleName")); currentVehicleLayer.Columns.Add(new FeatureSourceColumn("Duration")); Application.Current.Dispatcher.BeginInvoke(new Action(() => { lock (traceOverlay.Layers) { traceOverlay.Layers.Add(currentVehicle.Name, currentVehicleLayer); } })); } else { // Find the overlay in the map currentVehicleLayer = (InMemoryFeatureLayer)traceOverlay.Layers[currentVehicle.Name]; } currentVehicleLayer.InternalFeatures.Clear(); // Add the vehicle's latest position Feature latestPositionFeature = new Feature(currentVehicle.Location.GetLocation().GetWellKnownBinary(), currentVehicle.Name); latestPositionFeature.ColumnValues["DateTime"] = currentVehicle.Location.DateTime.ToString(CultureInfo.InvariantCulture); latestPositionFeature.ColumnValues["Speed"] = currentVehicle.Location.Speed.ToString(CultureInfo.InvariantCulture); latestPositionFeature.ColumnValues["Longitude"] = currentVehicle.Location.Longitude.ToString(CultureInfo.InvariantCulture); latestPositionFeature.ColumnValues["Latitude"] = currentVehicle.Location.Latitude.ToString(CultureInfo.InvariantCulture); latestPositionFeature.ColumnValues["VehicleName"] = currentVehicle.Name; latestPositionFeature.ColumnValues["Duration"] = currentVehicle.GetSpeedDuration().ToString(CultureInfo.InvariantCulture); currentVehicleLayer.InternalFeatures.Add(latestPositionFeature.Id, latestPositionFeature); } } }