Mujtaba
Mujtaba

Reputation: 369

C# Get current latitude and longitude in asp.net application

I'm working on an application that will detect the user location show latitude and longitude and show the physical address against them. When I run this, I get nothing.

My system location is on and has an active internet connection. I see every time a dot in the task bar that someone is tracking location but I still get nothing.

Here is my code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Device.Location;
using System.Diagnostics;
namespace APIsProject
{
public partial class GetLatitudeandLongitude : System.Web.UI.Page
{
    private string latitude;
    private string longitute;
    private GeoCoordinateWatcher watcher = new GeoCoordinateWatcher();
    protected void Page_Load(object sender, EventArgs e)
    {
        GeoCoordinateWatcher watcher = new GeoCoordinateWatcher();
        watcher = new GeoCoordinateWatcher();
        // Catch the StatusChanged event.  
        watcher.StatusChanged += Watcher_StatusChanged;
        // Start the watcher.  
        watcher.Start();

    }

    private void Watcher_StatusChanged(object sender, 
    GeoPositionStatusChangedEventArgs e) // Find GeoLocation of Device  
    {
        try
        {
            if (e.Status == GeoPositionStatus.Ready)
            {
                // Display the latitude and longitude.  
                if (watcher.Position.Location.IsUnknown)
                {
                    latitude = "0";
                    longitute = "0";
                }
                else
                {
                    latitude = 
       watcher.Position.Location.Latitude.ToString();
                    longitute = 
       watcher.Position.Location.Longitude.ToString();
                }
            }
            else
            {
                latitude = "0";
                longitute = "0";
            }
        }
        catch (Exception)
        {
            latitude = "0";
            longitute = "0";
        }
    }

    protected void InsertButton_Click(object sender, EventArgs e)
    {
         TextBoxLatitude.Text = latitude;
        TextBoxLongitude.Text = longitute;

    }
 }
}

Upvotes: 0

Views: 22547

Answers (2)

Sagar Jadhav
Sagar Jadhav

Reputation: 129

Simple Try This..It's Working...

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="location.WebForm2" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
   <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>    
    <script>
        function getLocation()
        {
            if (navigator.geolocation)
            {
                navigator.geolocation.getCurrentPosition(showPosition);
            } else
            {
                document.getElementById("Label1").value = "Geolocation is not supported by this browser.";
            }
        }
        function showPosition(position)
        {
            document.getElementById("TextBox1").value = position.coords.latitude
            document.getElementById("TextBox2").value = position.coords.longitude;
        }
</script>
<%--<button onclick="getLocation()">Try It</button>--%>
    <form id="form1" runat="server">     
        <div>
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server"  Text="Button" OnClientClick="getLocation();return false;" />
        <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
        </div>
    </form>   
</body>
</html>

Upvotes: 0

Rafał Developer
Rafał Developer

Reputation: 2055

You can try this to find position using navigator.geolocation.getCurrentPosition() :

<html>
<body>

<p>Click the button to get your coordinates.</p>

<button onclick="getLocation()">Try It</button>

<p id="demo"></p>

<script>
var x = document.getElementById("demo");

function getLocation() {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(showPosition);
    } else { 
        x.innerHTML = "Geolocation is not supported by this browser.";
    }
}

function showPosition(position) {
    x.innerHTML = "Latitude: " + position.coords.latitude + 
    "<br>Longitude: " + position.coords.longitude;
}
</script>

</body>
</html>

Live demo

Upvotes: 9

Related Questions