3

I want to get the latitude and longitude of current device location, here is my code that is not working. "coord.IsUnknown == false" always return true that's why i am not been able to get the latitude and longitude.

public partial class PoultryDirectory_HomeMaster : System.Web.UI.MasterPage
{
   protected void Page_Load(object sender, EventArgs e)
    {

    }


   protected void btnSingUp_Click(object sender, EventArgs e)
   {
       getLatLng();
   }

  private void getLatLng()
    {

        GeoCoordinateWatcher watcher = new GeoCoordinateWatcher();

        watcher.TryStart(false, TimeSpan.FromMilliseconds(1000));

        GeoCoordinate coord = watcher.Position.Location;

        if (coord.IsUnknown == false)
        {
            double lat = coord.Latitude;
            double lng = coord.Longitude;
            Console.WriteLine("Lat: {0}, Long: {1}",lat,lng);
        }
        else
        {
            Console.WriteLine("Unknown latitude and longitude.");
        }
    }
}
Usf Noor
  • 217
  • 1
  • 4
  • 16

1 Answers1

0

Make sure your laptop/Pc 's location settings is turned on.

 GeoCoordinateWatcher watcher = new GeoCoordinateWatcher();

            watcher.PositionChanged += (sender, e) =>
            {
                var coordinate = e.Position.Location;
                Console.WriteLine("Lat: {0}, Long: {1}", coordinate.Latitude,
                    coordinate.Longitude);
                // Uncomment to get only one event.
                // watcher.Stop(); 
            };

            // Begin listening for location updates.
            watcher.Start();
VVN
  • 1,607
  • 2
  • 16
  • 25