locked
windows.samples.location how do I tell when there is no gps signal? RRS feed

  • Question

  • Hi I have created an application that uses Microsoft.Windowsmobile.Samples.Location to get a gps position my program first checks to see if it can get a valid long/lat values if it cant after some time it gives a message saying so otherwise displays the location.

    My problem is that after I have got a valid position if i then move to an area I am sure not to get a signal and run again the program will give the same coordinates, checking with googlemaps no satalites can be seen so why does my app seem to think it can get valid data?

    the gps class doesnt seem to have a clear method otherwise I would do this after obtaining the desired information. 

    (oh just to note this happens also after exiting the app and loading it again, it only refreshes after leaving it for a while 10 mins or more)


    (on a compleately different side question I have created a second thread to test if it is still running I have added a volatile boolean which is changed once the thread finishes is this method correct/ok? I think with the full framework threads have a property called isalive and thought this was an ok compromise)
    Monday, July 20, 2009 10:53 PM

Answers

  • Ok well I have found a solution to my problem and that is to use the SatellitesInViewCount property to make sure there are at least 5 satalites which can be seen, This isnt perfect as there will be times a valid position can be determined with less satalites in view and I suppose the satalites in view may not have been used to determin the location. Anyway in the tests I have conducted it seems to adequitely solve my problem.
    • Marked as answer by badasschris Tuesday, August 4, 2009 3:05 PM
    Tuesday, August 4, 2009 3:04 PM

All replies

  • any 1 have any ideas? is it not a good idea to use this method? how else could I get gps data?
    Tuesday, July 21, 2009 10:05 AM
  • What can you get from Gps.GetPosition and Gps.GetDeviceState (or Gps.DeviceStateChanged event) when you believe there is no signal?
    Please mark the post that helps you, and unmark that does not. This benefits our community members.
    • Edited by warrentang Friday, July 24, 2009 9:17 AM
    Friday, July 24, 2009 9:15 AM
  • Hi   gps.getposition gives the exact same result that I received when I did have a signal, I havent actually checked device state although gps.open is called before I do getposition.

    The point of my app is to be able to get my current position on demand once and then turn off the gps receiver, as such I havent used the location changed event.

    I suppose I could have a function that opens the gps and then subscribes to the location changed event although I then have the problem when I actually am in the exact same position as the last check. any ideas?

    I will check the results of device state and post back.
    Friday, July 24, 2009 9:48 AM
  • I guess your app requirement is like the getting GPS location informations once when you want and not the polling for the GPS locations frequently.

    If this is right then you need to modify the Gps.cs of the sample as follows-

    1) No need of the locationChanged() and deviceStateChanged() events.

    2) No need of the CreateGpsEventThread().

    3) Need to update as follows-

            public static void Open()
            {
                //Open the GPS and get the handle. Using GPSOpenDevice() P/Invoke.
            }
    
            public static GpsPosition GetPosition(TimeSpan maxAge)
            {
                if (!Opened)
                {
                    //Open the GPS device
                }
                //Get the location by using the P/Invoke GPSGetPosition().
                //return GpsPosition;
            }
    
            public static void Close()
            {
                //Close the GPS by using GPSCloseDevice() P/Invoke.
            }
    Hope i understood you requirement.
    Please let me know if you still have any issues.

    Regards,
    amit rote
    • Proposed as answer by Amit Rote Friday, July 24, 2009 10:29 AM
    Friday, July 24, 2009 10:16 AM
  • I guess your app requirement is like the getting GPS location informations once when you want and not the polling for the GPS locations frequently.

    If this is right then you need to modify the Gps.cs of the sample as follows-

    1) No need of the locationChanged() and deviceStateChanged() events.

    2) No need of the CreateGpsEventThread().

    3) Need to update as follows-

            public static void Open()
    
            {
    
                //Open the GPS and get the handle. Using GPSOpenDevice() P/Invoke.
    
            }
    
    
    
            public static GpsPosition GetPosition(TimeSpan maxAge)
    
            {
    
                if (!Opened)
    
                {
    
                    //Open the GPS device
    
                }
    
                //Get the location by using the P/Invoke GPSGetPosition().
    
                //return GpsPosition;
    
            }
    
    
    
            public static void Close()
    
            {
    
                //Close the GPS by using GPSCloseDevice() P/Invoke.
    
            }
    
    
    Hope i understood you requirement.
    Please let me know if you still have any issues.

    Regards,
    amit rote

    Hi this is exactly my requirements, actually coding what you have suggested is my only issue but I will look into it thanks for the advice.
    • Proposed as answer by Amit Rote Friday, July 24, 2009 10:35 AM
    Friday, July 24, 2009 10:21 AM
  • 1) The method
        public static GpsPosition GetPosition(TimeSpan maxAge)
        you can use as it is in the sample application.

    2) Update Open() as follows-

        public static void Open()
            {
                try
                {
                    if (!Opened)
                    {
                        GPSHandle = GPSOpenDevice(IntPtr.Zero, IntPtr.Zero, null, 0);
                    }
                }
                catch
                {               
                    throw;
                }
            }
     
     Only the difference is that while opening the GPS using Open( ), no need of method to create the thread.
      
     3) Close will remain as it is.

    Thats it.

    Regards,
    amit rote
    • Proposed as answer by Amit Rote Friday, July 24, 2009 10:29 AM
    • Marked as answer by warrentang Monday, July 27, 2009 2:22 AM
    • Unmarked as answer by badasschris Friday, July 31, 2009 1:42 PM
    Friday, July 24, 2009 10:29 AM
  • 1) The method
        public static GpsPosition GetPosition(TimeSpan maxAge)
        you can use as it is in the sample application.

    2) Update Open() as follows-

        public static void Open()
            {
                try
                {
                    if (!Opened)
                    {
                        GPSHandle = GPSOpenDevice(IntPtr.Zero, IntPtr.Zero, null, 0);
                    }
                }
                catch
                {               
                    throw;
                }
            }
     
     Only the difference is that while opening the GPS using Open( ), no need of method to create the thread.
      
     3) Close will remain as it is.

    Thats it.

    Regards,
    amit rote

    Thanks very much I'll do as you say and give it a go. Ill post back if it fixes the problem. (be a little while I live in a flat in a tall building so I'll need to run downstairs and run around getting some gps data)
    Friday, July 24, 2009 10:32 AM
  • Hi sorry for late reply, I have been testing and this dosent solve my problem actually doesnt seem to make any difference. In my program I wasnt using the GpsPosition.GetPosition(TimeSpan maxAge) only the GetPosition() method, strangely when using a max age value even of 10 mins the program never reports a gps position.

    I have tried using the value given by GpsPosition.Time but I get an exception saying that the returned value is out of range which I find a bit confusing.

    I will keep at it and post back if I have any success.

    my code looks like this

    public Boolean getLocation()
            {
                if (!gps.Opened)
                {
                    return false;
                }
               
                GpsPosition position = gps.GetPosition(TimeSpan.FromSeconds(300));
                if (position == null)
                {
                    return false;
                }

    Anything wrong with this part of the code?
    Friday, July 31, 2009 1:48 PM
  • The same i am using and its working fine for me.

    Now I am little bit confused about the >> I get an exception saying that the returned value is out of range.

    Would be better if you post your total GPS module. So that i can look into it and test it on my side.


    Thanks and Regards, Amit Rote
    Tuesday, August 4, 2009 7:42 AM
  • Hi again Amit I really appreciate you having a look at my problem, I have rewritten the program from scratch to see if I can eliminate any bugs.

    I beleive I may have figured out why I cannot use the gps.getPosition(maxTime), it seems as though this function compaires the last obtained gps time with the phones system time, the problem with this is that if the users system time is wrong then this method wont work, for example if i get a gps fix and the have a function which prints out the GpsPosition.time and the DateTime.now values they will be different for me by 1 hour + a few mins (the one hour must be because of BST and the few mins are usual inacuricies in time)

    So what I will now do is look through the sample code and see if I can chnage how it gets its position fix time, if i get it from the system time then I should be able to determin more precicely when a valid signal was obtained.

    **note**

    about the out of range exception I was getting this was the code causing it (where lastFix is set earlier using DateTime.Now)

    if (position.TimeValid)
                {
                    if (position.Time.CompareTo(lastFix) < 0)
                    {
                        foundLocation = false;
                    }
                }

    *****

    I find it strange that there isnt a property that says wether or not there is a position fix right now, that would have solved all my problems, worst case I could make sure at least 5 satalites can be seen I suppose but I can see this causing problems.


    **EDIT**


    Ok after looking through the code to do what I have suggested I would have to change the code of the GPS Intermediate Driver and I don't think this will be possible or practical as am I right in thinking this is part of the compact framework and therefore if I could chnage it anyone wanting to use my application would also have to chnage theirs? which could also mess up other applications.

    I don't see how the maxAge parameter is useful as from what I can see there is no guarentee that the users deviuce will have the correct time.
    Tuesday, August 4, 2009 2:32 PM
  • Ok well I have found a solution to my problem and that is to use the SatellitesInViewCount property to make sure there are at least 5 satalites which can be seen, This isnt perfect as there will be times a valid position can be determined with less satalites in view and I suppose the satalites in view may not have been used to determin the location. Anyway in the tests I have conducted it seems to adequitely solve my problem.
    • Marked as answer by badasschris Tuesday, August 4, 2009 3:05 PM
    Tuesday, August 4, 2009 3:04 PM
  • Hi,

    How would you find the satellite count  ?

    Monday, May 5, 2014 7:19 AM