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;
}
}
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.