首页 > 解决方案 > GeoLocation: Obtaining precise Latitude and Longitude in Windows Desktop

问题描述

Is there a way to get the Latitude and Longitude of a Windows laptop without any fancy internal gadget. The only condition about this is that the laptop may be connected by Ethernet and/or wifi.

Actually, this web application does exactly what I am looking for: My Current Location As you can see, this is not just an IP-City Location, it is very precise.

enter image description here

The page claims that they accomplish this by Geolocation "using the latest web technology called HTML5 / w3C Geolocation".

This is way more than I need... Let me put you in context, what I am trying to do here is to get the precise Latitude and Longitude just as this app does but through a Windows Service when the machine turns on and it connects to the internet. Basically 2 string variables, just that. Then I will send them to a remote API.

I have this code example from a blog. Is not my code, I downloaded the project but it seems to do nothing (this is all the code you need).

using System.Device.Location;

namespace howto_location_lat_long
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        // The coordinate watcher.
        private GeoCoordinateWatcher Watcher = null;

        // Create and start the watcher.
        private void Form1_Load(object sender, EventArgs e)
        {
            // Create the watcher.
            Watcher = new GeoCoordinateWatcher();

            // Catch the StatusChanged event.
            Watcher.StatusChanged += Watcher_StatusChanged;

            // Start the watcher.
            Watcher.Start();
        }

        // The watcher's status has change. See if it is ready.
        private void Watcher_StatusChanged(object sender, GeoPositionStatusChangedEventArgs e)
        {
            if (e.Status == GeoPositionStatus.Ready)
            {
                // Display the latitude and longitude.
                if (Watcher.Position.Location.IsUnknown)
                {
                    txtLat.Text = "Cannot find location data";
                }
                else
                {
                    GeoCoordinate location =
                        Watcher.Position.Location;
                    txtLat.Text = location.Latitude.ToString();
                    txtLong.Text = location.Longitude.ToString();
                }
            }
        }
    }
}

It does not work for me hehe, the tutorial guy claimed it does work.

Whatever... Do you guys know a way to accomplish this?, the background language is not important, it could be Java, C#, NodeJs...

I need to: 1. When the computer connects to internet execute the "SendLocationMethod" 2. Inside the method: Obtain precise Latitude and Longitude 3. Send both to a WebApi service

I've read possibilities like: 1. Use the navigator cookies to obtain the location, but this has to be done with the current location when the machine turns on... If it is necessary to force-open the web browsers at the moment machine turns on is not important. 2. Internal coding. 3. Tracing laptop location with Wifi signals (This one does not suit as a solution for me). 4. Use my current Public Ip and sent to an API that will return my PRECISE location, not the city or zone.

I accept any kind of suggestion that suits what I am looking for. As long they fit my requirements:

> If Computer connects to the Internet then Obtain Latitude and Longitude then send both to a Service I already have ready.

EDIT: It could be when the computer turns on too. (For the scenario that it is required to force open the web browser at window start), but this one is really dirty.

标签: javac#geolocation

解决方案


推荐阅读