Example #1
0
        /// <summary>
        /// Get the position reported by the GPS receiver that is no older than
        /// the maxAge passed in
        /// </summary>
        /// <param name="maxAge">Max age of the gps position data that you want back.
        /// If there is no data within the required age, null is returned.
        /// if maxAge == TimeSpan.Zero, then the age of the data is ignored</param>
        /// <returns>GpsPosition class with all the position details</returns>
        public GpsPosition GetPosition(TimeSpan maxAge)
        {
            GpsPosition gpsPosition = null;

            if (Opened)
            {
                // allocate the necessary memory on the native side.  We have a class (GpsPosition) that
                // has the same memory layout as its native counterpart
                IntPtr ptr = Utils.LocalAlloc(Marshal.SizeOf(typeof(GpsPosition)));

                // fill in the required fields
                gpsPosition           = new GpsPosition();
                gpsPosition.dwVersion = 1;
                gpsPosition.dwSize    = Marshal.SizeOf(typeof(GpsPosition));

                // Marshal our data to the native pointer we allocated.
                Marshal.StructureToPtr(gpsPosition, ptr, false);

                // call native method passing in our native buffer
                int result = GPSGetPosition(gpsHandle, ptr, 500000, 0);
                if (result == 0)
                {
                    // native call succeeded, marshal native data to our managed data
                    gpsPosition = (GpsPosition)Marshal.PtrToStructure(ptr, typeof(GpsPosition));

                    if (maxAge != TimeSpan.Zero)
                    {
                        // check to see if the data is recent enough.
                        if (!gpsPosition.TimeValid || DateTime.Now - maxAge > gpsPosition.Time)
                        {
                            gpsPosition = null;
                        }
                    }
                }

                // free our native memory
                Utils.LocalFree(ptr);
            }

            return(gpsPosition);
        }
Example #2
0
        static void gps_LocationChanged(object sender, LocationChangedEventArgs args)
        {
            position = args.Position;

            // call the UpdateData method via the updateDataHandler so that we
            // update the UI on the UI thread
            if (updatePositionDataHandler != null)
            {
                updatePositionDataHandler.Invoke(sender, args);
            }
            //if (lastSave == null)
            //{
            //    GPSData point = new GPSData(position.Longitude, position.Latitude);
            //    //dataList.Add(point);
            //    lastSave = DateTime.Now;
            //}
            //if ((DateTime.Now.Millisecond - lastSave.Millisecond) > 10000)
            //{
            //    GPSData point = new GPSData(position.Longitude, position.Latitude);
            //    dataList.Add(point);
            //    lastSave = DateTime.Now;
            //}
            //System.Threading.Thread.Sleep(3000);
        }
        static void gps_LocationChanged(object sender, LocationChangedEventArgs args)
        {
            position = args.Position;

            // call the UpdateData method via the updateDataHandler so that we
            // update the UI on the UI thread
            if (updatePositionDataHandler != null)
            {
                updatePositionDataHandler.Invoke(sender, args);
            }
            //if (lastSave == null)
            //{
            //    GPSData point = new GPSData(position.Longitude, position.Latitude);
            //    //dataList.Add(point);
            //    lastSave = DateTime.Now;
            //}
            //if ((DateTime.Now.Millisecond - lastSave.Millisecond) > 10000)
            //{
            //    GPSData point = new GPSData(position.Longitude, position.Latitude);
            //    dataList.Add(point);
            //    lastSave = DateTime.Now;
            //}
            //System.Threading.Thread.Sleep(3000);
        }
 public LocationChangedEventArgs(GpsPosition position)
 {
     this.position = position;
 }
Example #5
0
 public LocationChangedEventArgs(GpsPosition position)
 {
     this.position = position;
 }
Example #6
0
        /// <summary>
        /// Get the position reported by the GPS receiver that is no older than
        /// the maxAge passed in
        /// </summary>
        /// <param name="maxAge">Max age of the gps position data that you want back. 
        /// If there is no data within the required age, null is returned.  
        /// if maxAge == TimeSpan.Zero, then the age of the data is ignored</param>
        /// <returns>GpsPosition class with all the position details</returns>
        public GpsPosition GetPosition(TimeSpan maxAge)
        {
            GpsPosition gpsPosition = null;
            if (Opened)
            {
                // allocate the necessary memory on the native side.  We have a class (GpsPosition) that
                // has the same memory layout as its native counterpart
                IntPtr ptr = Utils.LocalAlloc(Marshal.SizeOf(typeof(GpsPosition)));

                // fill in the required fields
                gpsPosition = new GpsPosition();
                gpsPosition.dwVersion = 1;
                gpsPosition.dwSize = Marshal.SizeOf(typeof(GpsPosition));

                // Marshal our data to the native pointer we allocated.
                Marshal.StructureToPtr(gpsPosition, ptr, false);

                // call native method passing in our native buffer
                int result = GPSGetPosition(gpsHandle, ptr, 500000, 0);
                if (result == 0)
                {
                    // native call succeeded, marshal native data to our managed data
                    gpsPosition = (GpsPosition)Marshal.PtrToStructure(ptr, typeof(GpsPosition));

                    if (maxAge != TimeSpan.Zero)
                    {
                        // check to see if the data is recent enough.
                        if (!gpsPosition.TimeValid || DateTime.Now - maxAge > gpsPosition.Time)
                        {
                            gpsPosition = null;
                        }
                    }
                }

                // free our native memory
                Utils.LocalFree(ptr);
            }

            return gpsPosition;
        }