public VehicleViewModel(Vehicle vehicle, MapModel mapModel)
            : base()
        {
            this.vehicle = vehicle;
            this.ownerName = vehicle.Name;
            this.mapModel = mapModel;
            this.iconUri = string.Format(CultureInfo.InvariantCulture, vehicleImageFormat, vehicle.IconPath);

            this.Load();
        }
        private Vehicle GetCurrentVehicle(int vehicleId, DateTime currentTime, TimeSpan trackHistoryVehicleTimeSpan)
        {
            string sql = "SELECT A.VehicleName, A.VehicleID, A.VehicleIconVirtualPath, B.Longitude, B.Latitude, B.[Date], B.Speed FROM (Vehicle A LEFT OUTER JOIN Location B ON A.VehicleID = B.VehicleID) WHERE (A.VehicleID = {0}) AND (B.[Date] <= #{1}# and B.[Date]>=#{2}#) ORDER BY A.VehicleID, B.[Date] DESC";
            DateTime trackStartTime = currentTime.AddTicks(-trackHistoryVehicleTimeSpan.Ticks);
            sql = String.Format(CultureInfo.InvariantCulture, sql, vehicleId, currentTime.ToString(CultureInfo.InvariantCulture), trackStartTime.ToString(CultureInfo.InvariantCulture));

            Vehicle currentVechicle = new Vehicle(vehicleId);
            DataSet currentLocations = null;
            try
            {
                // Get the locations from current time back to the passed time span
                currentLocations = ExecuteQuery(sql);
                Collection<double> historySpeeds = new Collection<double>();
                for (int rowIndex = 0; rowIndex < currentLocations.Tables[0].Rows.Count; rowIndex++)
                {
                    DataRow dataRow = currentLocations.Tables[0].Rows[rowIndex];
                    currentVechicle.IconPath = dataRow["VehicleIconVirtualPath"].ToString();

                    double latitude = Convert.ToDouble(dataRow["Latitude"], CultureInfo.InvariantCulture);
                    double longitude = Convert.ToDouble(dataRow["Longitude"], CultureInfo.InvariantCulture);
                    double speed = Convert.ToDouble(dataRow["Speed"], CultureInfo.InvariantCulture);
                    DateTime dateTime = Convert.ToDateTime(dataRow["Date"], CultureInfo.InvariantCulture);
                    Location currentLocation = new Location(longitude, latitude, speed, dateTime);
                    historySpeeds.Add(speed);

                    if (rowIndex == 0)
                    {
                        string vehicleName = dataRow["VehicleName"].ToString();
                        currentVechicle.Location = currentLocation;
                        currentVechicle.Id = vehicleId;
                        currentVechicle.Name = vehicleName;
                    }
                    else
                    {
                        currentVechicle.HistoryLocations.Add(currentLocation);
                    }
                }
            }
            finally
            {
                if (currentLocations != null)
                {
                    currentLocations.Dispose();
                }
            }

            return currentVechicle;
        }
        private bool IsInSpatialFence(Vehicle vehicle)
        {
            InMemoryFeatureLayer spatialFenceLayer = (InMemoryFeatureLayer)mapModel.SpatialFenceOverlay.Layers["SpatialFenceLayer"];

            // Get the point shape and then check if it is within any of the sptail fences using the QueryTools
            PointShape pointShape = new PointShape(vehicle.Location.Longitude, vehicle.Location.Latitude);
            bool isInSpatialFence = false;
            lock (spatialFenceLayer)
            {
                spatialFenceLayer.Open();
                Collection<Feature> spatialFencesWithin = spatialFenceLayer.QueryTools.GetFeaturesContaining(pointShape, ReturningColumnsType.NoColumns);
                if (spatialFencesWithin.Count > 0)
                {
                    isInSpatialFence = true;
                }
            }
            return isInSpatialFence;
        }