Aspx or mvc page to display browser latitude and longitude and bing adress

Anonymous
2023-07-17T14:02:04.48+00:00

I want to combine these two examples and create a aspx page to display the browser's lat/long and current address kindly please help

https://www.w3schools.com/html/tryit.asp?filename=tryhtml5_geolocation

  static async Task<string> ReverseGeocode(double latitude, double longitude)
        {
           // string BingMapsApiKey = "bing key";

            using (HttpClient client = new HttpClient())
            {
                string url = $"https://dev.virtualearth.net/REST/v1/Locations/{latitude},{longitude}?o=json&key={BingMapsApiKey}";
                HttpResponseMessage response = await client.GetAsync(url);
                string json = await response.Content.ReadAsStringAsync();

                // Parse the JSON response to extract the address
                dynamic data = Newtonsoft.Json.JsonConvert.DeserializeObject(json);
                string address = data.resourceSets[0].resources[0].address.formattedAddress;

                return address;
            }
        }
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,515 questions
0 comments No comments
{count} votes

Accepted answer
  1. Lan Huang-MSFT 29,826 Reputation points Microsoft Vendor
    2023-07-18T03:26:56.0466667+00:00

    Hi @KALYANA ALLAM,

    I used the Ajax Post method on the Aspx page to achieve your request, you can refer to it.

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <script src="Scripts/jquery-3.4.1.min.js"></script>
        <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) {
    
                $.ajax({
                    url: "/WebForm11.aspx/ReverseGeocode",
                    method: "POST",
                    contentType: "application/json; charset=utf-8",
                    data: '{"latitude":"' + position.coords.latitude + '","longitude":"' + position.coords.longitude + '"}',
                    success: function (response) {
                        document.getElementById("Label3").innerHTML = response.d["Result"];
    
                        document.getElementById("Label1").innerHTML = position.coords.latitude;
                        document.getElementById("Label2").innerHTML = position.coords.longitude
    
                    }
                })
    
            }
    
        </script>
    </head>
    <body>
        <button onclick="getLocation()">GetLocation</button>
        <h3>latitude</h3>
        <asp:Label ID="Label1" runat="server"></asp:Label>
        <h3>longitude</h3>
        <asp:Label ID="Label2" runat="server"></asp:Label>
        <h3>address</h3>
        <asp:Label ID="Label3" runat="server"></asp:Label>
    </body>
    </html>
    
    
     [WebMethod]
            public static async Task<string> ReverseGeocode(double latitude, double longitude)
            {
    
    
                using (HttpClient client = new HttpClient())
                {
                    string url = $"https://dev.virtualearth.net/REST/v1/Locations/{latitude},{longitude}?o=json&key=****";
                    var response = client.GetAsync(url).Result;
    
                    string json = await response.Content.ReadAsStringAsync();
    
                    // Parse the JSON response to extract the address
                    dynamic data = Newtonsoft.Json.JsonConvert.DeserializeObject(json);
                    string address = data.resourceSets[0].resources[0].address.formattedAddress;
    
                    return address;
                }
            }
    

    enter image description here Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


1 additional answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 67,016 Reputation points
    2023-07-17T15:50:18.11+00:00

    you need a sniffer page to send the data to the server

    <form action="ShowGeoLocation.aspx" id="GeoForm">
    <input type="hidden" name="Latitude" id="Latitude">
    <input type="hidden" name="Longitude" name="Longitude">
    </form>
    <script>
    navigator.geolocation?.getCurrentPosition(function (position) {
      document.getElementById("Latitude").value = position.coords.latitude;
      document.getElementById("Longitude").value = position.coords.longitude;
      document.getElementById("GeoForm").submit();
    });
    <script>
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.