コード例 #1
0
 /// <summary>Raises the <see cref="Position"/> event</summary>
 /// <param name="e">Event arguments</param>
 protected virtual void OnPosition(GpsPositionEventArgs e)
 {
     if (Position != null)
     {
         Position(this, e);
     }
 }
コード例 #2
0
ファイル: TrackerForm.cs プロジェクト: mendhak/gpstracka
        /// <summary>Called when GPS position is received</summary>
        /// <param name="sender">A GPS provider</param>
        /// <param name="e">Event arguments containing actual GPS position</param>
        void GpsPositionReceived(GpsProvider sender, GpsPositionEventArgs e)
        {
            GpsPosition pos = e.Position;
            if (waitForStop || !gpsRunning)
            {
                return;
            }

            if ((DateTime.Now - lastGpsLoggedAt).TotalMilliseconds < 10)
            {
                return; //I've experianced multiple logs at same place in same time
            }

            if (pos.Longitude.HasValue && pos.Latitude.HasValue)
            {
                afterStart = false;
                invalidPositionCount = 0;
                lastGpsLoggedAt = DateTime.Now;
                DateTime time = AdvancedConfig.UseGpsTime ? pos.GpsTime.ToLocalTime() : DateTime.Now;
                decimal? altitude = pos.Altitude.HasValue ? pos.Altitude + (decimal)AdvancedConfig.AltitudeCorrection : null;

                string gpsData = string.Format(
                    AdvancedConfig.TextLogFormat,
                    time.ToUniversalTime(),
                    Math.Round(pos.Longitude.Value, 7),
                    Math.Round(pos.Latitude.Value, 7),
                    altitude.HasValue ? Math.Round(altitude.Value, 7) : 0,
                    time,
                    AdvancedConfig.ElevationUnitName,
                    statistic == null ? 0.0M : statistic.SumLength * AdvancedConfig.DistanceMultiplier,
                    AdvancedConfig.DistanceUnitName
                );

                if (!positionLogged)
                {
                    bool logThisPoint = false; //Check if the point is not too neer to stop-start point
                    decimal? distance = null;
                    if (lastPositionA == null)
                    {
                        //Stop-start point unknown (immediately after start)
                        logThisPoint = true;
                    }
                    else
                    {
                        distance = Math.Abs(GpsPosition.CalculateDistance(lastPositionA.Value, pos, OpenNETCF.IO.Serial.GPS.Units.Kilometers) * 1000);
                        if (distance < AdvancedConfig.MinimalDistance)
                        {
                            //Point is to near
                            lastPositionC = pos; //Remember it
                            lastTimeC = time;    //And remember the time
                        }
                        else
                        {
                            //Points are far enough
                            logThisPoint = true;//Log this point
                            if (lastPositionC != null && time != lastTimeA && lastTimeA != lastTimeC)
                            {
                                WriteToFile(
                                    lastTimeC,
                                    Convert.ToDouble(Math.Round(lastPositionC.Value.Latitude.Value, 7)),
                                    Convert.ToDouble(Math.Round(lastPositionC.Value.Longitude.Value, 7)),
                                    Convert.ToDouble(Math.Round(lastPositionC.Value.Altitude.HasValue ? lastPositionC.Value.Altitude.Value + AdvancedConfig.AltitudeCorrection : 0, 7))
                                );
                            }
                        }
                    }

                    WriteToTextbox(gpsData + (logThisPoint ? "" : " X"));
                    OnPoint(time, new GpsPoint(pos.Latitude.Value, pos.Longitude.Value, pos.Altitude), logThisPoint);

                    if (logThisPoint)
                    {
                        if (distance.HasValue)
                        {
                            ShowSpeed(distance.Value / 1000 / (decimal)(time - lastTimeA).TotalHours);
                        }

                        Status(Properties.Resources.status_PositionReceived);
                        lastPositionA = pos; //Remember position
                        lastTimeA = time;    //Remember time
                        lastPositionC = pos;
                        lastTimeC = time;
                        WriteToFile(
                            time,
                            Convert.ToDouble(Math.Round(pos.Latitude.Value, 7)),
                            Convert.ToDouble(Math.Round(pos.Longitude.Value, 7)),
                            Convert.ToDouble(altitude.HasValue ? Math.Round(altitude.Value, 7) : 0));

                    }
                    else
                    {
                        Status(string.Format(Properties.Resources.PointsTooNear, distance));
                    }

                    StopGps();
                }
                positionLogged = true;
            }
            else
            {
                if (++invalidPositionCount == int.MaxValue)
                {
                    invalidPositionCount = 0;
                }

                if (!afterStart && AdvancedConfig.InvalidPositionsMax != 0 && invalidPositionCount >= AdvancedConfig.InvalidPositionsMax)
                {
                    invalidPositionCount = 0;
                    Status(string.Format(Properties.Resources.status_InvalidPosWait, invalidPositionCount));
                    StopGps();
                }
                else if (afterStart || AdvancedConfig.InvalidPositionsMax == 0)
                {
                    if ((DateTime.Now - lastInvPosUserTime).TotalMilliseconds >= 900)
                    {
                        Status(string.Format("inv. pos. {0}", invalidPositionCount));
                        lastInvPosUserTime = DateTime.Now;
                    }
                }
                else
                {
                    if ((DateTime.Now - lastInvPosUserTime).TotalMilliseconds >= 900)
                    {
                        Status(string.Format(Properties.Resources.status_InvalidPos, invalidPositionCount));
                        lastInvPosUserTime = DateTime.Now;
                    }
                }

            }
        }
コード例 #3
0
ファイル: GpsProvider.cs プロジェクト: mendhak/gpstracka
 /// <summary>Raises the <see cref="Position"/> event</summary>
 /// <param name="e">Event arguments</param>
 protected virtual void OnPosition(GpsPositionEventArgs e)
 {
     if (Position != null) Position(this, e);
 }