none
How can I get the latitude and longitude of a device? RRS feed

  • Question

  • How can I get the latitude and longitude of a device, I want to obtain the geolocation data of a device from a console application in C # and to do so use the method GeoCoordinateWatcher (); and it works only if the device has GPS, if the device does not have GPS does not send data, try to do it with an API that returned Latitude and longitude but the discordance is too much and it does not bring me the exact data, it occurred to me that it could be obtained the location from the browser, the question is How can I get the location from the browser?
    Monday, January 14, 2019 3:51 PM

All replies

  • Good morning, Jose.

    I'm not sure I understand the details of your question but I do understand the process of acquiring geocoordinates … I currently do it literally thousands of times each day.  I have an app which reads anywhere from 600 to 800 instruments every few minutes.

    While these days (since my retirement) I work exclusively with data found on the internet, several years ago I worked up-close-and-personal with the instruments .. i.e. a direct connection over a serial or ethernet connection.

    Either way, direct connection or web-resident data, my strategy remained the same …

    1. Acquire the data in the form of a single long string.

    2. write a RegEx to acquire the geocoordinates from the string.

    3. Do what'cha will with your newly acquired data.

    Pretty simple, huh?    If it wasn't simple, I wouldn't be able to do it.

    You mentioned getting the data from a browser …. Do-able I guess, but I think that's unnecessary, and worse, complicates the process.   I just wrote a single, small function which accepts a URL as a string argument, uses HttpClient method to read in the designated webpage, and then applies a RegEx (or Regexes in those cases where the geocoordinate format may vary from the conventional).

    And a final note regarding the Regex you write: Context :  be certain that your Regex takes context into consideration when acquiring coordinates, otherwise you might inadvertently collect data which aren't coordinates but simply appear in a format similar to that of geocoordinates.

    The Good News is that (assuming I understood the question) what you want to do is easily achieved. HttpClient and the RegEx do all the heavy lifting.

    Monday, January 14, 2019 5:55 PM
  • Sorry if I did not understand, what I want is to get the latitude and longitude of a device like this does not have a GPS, for example a desktop computer, the APIs that I used are not accurate enough.
    Monday, January 14, 2019 7:28 PM
  • Hello again, Jose.

    Well, clearly, I owe you an apology.  I shot my big mouth off without fully understanding the question. 

    I now believe you seek a method to determine the geocoordinates of a device which does not have access to a GPS, or other (GLONASS, ARGO, RAYDIST, LORAN, etc.) receiver  (are RAYDIST & LORAN even a 'thing' anymore ?).

    Regrettably, I don't have an answer for you. Indeed, I can't imagine how any methodology could determine coordinates of a remote device which does not have a GPS or other positioning receiver / transceiver.  After all, to determine position, one must have inputs from 3 or 4 satellites (in the case of GPS).

    Sorry to disappoint you, Jose. I thought I had the answer, but now see that I did not fully understand your question.

    Perhaps another reader can offer a suggestion.

    • Edited by Lincoln_MA Monday, January 14, 2019 10:45 PM
    Monday, January 14, 2019 10:42 PM
  • Hello again, Jose.

    Well, clearly, I owe you an apology.  I shot my big mouth off without fully understanding the question. 

    I now believe you seek a method to determine the geocoordinates of a device which does not have access to a GPS, or other (GLONASS, ARGO, RAYDIST, LORAN, etc.) receiver  (are RAYDIST & LORAN even a 'thing' anymore ?).

    Regrettably, I don't have an answer for you. Indeed, I can't imagine how any methodology could determine coordinates of a remote device which does not have a GPS or other positioning receiver / transceiver.  After all, to determine position, one must have inputs from 3 or 4 satellites (in the case of GPS).

    Sorry to disappoint you, Jose. I thought I had the answer, but now see that I did not fully understand your question.

    Perhaps another reader can offer a suggestion.


    Yes, I believe Lincoln is correct.  :)

    There is no way I know of to get a location from a " a desktop computer".

    Perhaps you can explain how you think that is done or is that the basic question?

    Do you mean look up a location on the web from a database of known locations?

    You should explain what "the APIs that I used are not accurate enough." means exactly.

    And then finally how accurate do you want to be? 

    Monday, January 14, 2019 11:32 PM
  • Hi Jose Nino,

    Thank you for posting here.

    Based on your description, I create a simple console application (code as below) by using GeoCoordinateWatcher, it works on my side, please take a look and check if it works for you.

    If the issue still exists, please feel free let me know.

    class Program
        {
            static void Main(string[] args)
            {
                CLocation myLocation = new CLocation();
                myLocation.GetLocationEvent();
                Console.ReadKey();
            }
            class CLocation
            {
                GeoCoordinateWatcher watcher;
    
                public void GetLocationEvent()
                {
                    this.watcher = new GeoCoordinateWatcher();
                    this.watcher.PositionChanged += new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(watcher_PositionChanged);
                    bool started = this.watcher.TryStart(false, TimeSpan.FromMilliseconds(2000));
                    if (!started)
                    {
                        Console.WriteLine("GeoCoordinateWatcher timed out on start.");
                    }
                }
    
                void watcher_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
                {
                    PrintPosition(e.Position.Location.Latitude, e.Position.Location.Longitude);
                }
    
                void PrintPosition(double Latitude, double Longitude)
                {
                    Console.WriteLine("Latitude: {0}, Longitude {1}", Latitude, Longitude);
                }
            }
    
    }
    

    Hope my suggestion could be helpful.

    Best regards,

    Jack



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, January 15, 2019 3:12 AM
    Moderator
  • Hi Jack.

    I noticed that your code is remarkably similar to the example in Microsoft Docs (https://docs.microsoft.com/en-us/dotnet/api/system.device.location.geocoordinatewatcher?view=netframework-4.7.2)

    Regrettably it SEEMS to have left out the most crucial part : where does this function acquire the position (or even the satellite signals which would permit a function to calculate position)?

    My understanding of the class documentation (https://docs.microsoft.com/en-us/windows/desktop/sensorsapi/introduction-to-the-sensor-and-location-platform-in-windows) is that the process expects a 'current local provider'. Note the section titled "About Sensor Devices".

    Therein lies the problem:  Jose seems to be saying that he wants this function to acquire lat, lon from a device (a PC) which is not quipped with a GPS Sensor.

    Without access to the Satellites or a GPS satellite receiver, it's difficult to see how this function can calculate (or read) lat, lon.

    I would direct Jose to Amazon.com … they offer a number of relatively inexpensive GPS receivers designed for PCs.

    Jack, I just came off a long medical procedure. I'm Wiped Out. However, I'll be back in touch tomorrow morning, perhaps VERY early tomorrow morning. I'm interested in hearing how this function works without a GPS sensor to interrogate.

    Tuesday, January 15, 2019 4:36 AM
  • Good day for everyone,
    To be more specific, basically if the computer has internet access,  maybe i can get the current position from the browser,  rigth?, or i'm wrong, if it is not, i have to use an API?, to answer the question of Tommytwotrain, saying than the APIs are not precise enough, it is because using them it brings me the position of my internet provider when i give my public ip and not is what i really need, thanks to everyone for the answers

    (Sorry for my english, it's so bad)

    • Edited by Jose Nino Tuesday, January 15, 2019 2:10 PM
    Tuesday, January 15, 2019 2:03 PM
  • Good day for everyone,
    To be more specific, basically if the computer has internet access,  maybe i can get the current position from the browser,  rigth?, or i'm wrong, if it is not, i have to use an API?, to answer the question of Tommytwotrain, saying than the APIs are not precise enough, it is because using them it brings me the position of my internet provider when i give my public ip and not is what i really need, thanks to everyone for the answers

    (Sorry for my english, it's so bad)


    Jose,

    "saying than the APIs are not precise enough, it is because using them it brings me the position of my internet provider when i give my public ip and not is what i really need, "

    I see.

    I think there are free services you can use.

    Here are examples that are vb but you can convert it to see if you can use it.

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/18dba336-5510-4bc7-a101-20edd49a36d4/ips-physical-location?forum=vbgeneral

    Hmm. Still seems goofy?

    I still wonder how accurate it is and how accurate you need to be? I mean within a kilometer? A meter?

    Anyone else?


    Tuesday, January 15, 2019 3:02 PM

  • I still wonder how accurate it is and how accurate you need to be? I mean within a kilometer? A meter?

    tommytwotrain

    The more precise possible, in meters if is possible.

    Thanks, i gonna see the code.


    Tuesday, January 15, 2019 3:55 PM
  • Hi Jose,

    How about the issue, do you try the method that I provided? If you have any other question, please feel free to contact us.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, January 29, 2019 8:25 AM
    Moderator