I dont have an Idea How to take latitude and longitude dynamically pls help me to solve..
Loading
I dont have an Idea How to take latitude and longitude dynamically pls help me to solve..
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Naimish MakwanaRank #324 · 7.7K reputationPosted Jun 2, 2023, 2:47 AM EDT
To obtain the user's location dynamically in a C# web application, you can use the Geolocation API provided by modern web browsers and then send the latitude and longitude to your server-side C# code. Here's an example:
Geolocation Example function getLocation() { if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(sendPosition); } else { console.log("Geolocation is not supported by this browser."); } } function sendPosition(position) { var latitude = position.coords.latitude; var longitude = position.coords.longitude; var xhr = new XMLHttpRequest(); xhr.open("POST", "/Home/SaveLocation", true); xhr.setRequestHeader("Content-Type", "application/json"); xhr.send(JSON.stringify({ latitude: latitude, longitude: longitude })); } Get LocationIn your server-side C# code (e.g., ASP.NET MVC or ASP.NET Core), create a controller action to receive the latitude and longitude:
using Microsoft.AspNetCore.Mvc; public class HomeController : Controller { [HttpPost] public IActionResult SaveLocation(double latitude, double longitude) { // Handle the latitude and longitude here (e.g., save it to a database) return Ok(); } }Or you can create your own method to get data to server.
Thanks
Rajkiran SwainRank #165 · 14.4K reputationPosted Jun 2, 2023, 1:33 AM EDT
if (navigator.geolocation) { navigator.geolocation.getCurrentPosition( function(position) { var latitude = position.coords.latitude; var longitude = position.coords.longitude; // Use latitude and longitude values here console.log("Latitude: " + latitude); console.log("Longitude: " + longitude); }, function(error) { // Handle error case console.error("Error getting geolocation:", error); } ); } else { console.error("Geolocation is not supported by this browser."); }Naimish MakwanaRank #324 · 7.7K reputationPosted Jun 2, 2023, 1:57 AM EDT
To obtain latitude and longitude dynamically in C#, you can use the Geolocation API provided by the .NET framework. Here's an example of how you can retrieve latitude and longitude using this API:
First, make sure you have the necessary NuGet package installed. In Visual Studio, right-click on your project, select "Manage NuGet Packages," search for "System.Device.Location," and install it.
Once you have the package installed, you can use the following code to obtain the latitude and longitude:
using System; using System.Device.Location; class Program { static void Main() { GeoCoordinateWatcher watcher = new GeoCoordinateWatcher(); watcher.PositionChanged += (sender, e) => { var coordinate = e.Position.Location; double latitude = coordinate.Latitude; double longitude = coordinate.Longitude; Console.WriteLine("Latitude: " + latitude); Console.WriteLine("Longitude: " + longitude); }; watcher.Start(); Console.WriteLine("Press any key to stop..."); Console.ReadKey(); watcher.Stop(); } }Thanks
TarunRank #6,374 · 268 reputationPosted Jun 2, 2023, 1:47 AM EDT
i want in C# sir
TarunRank #6,374 · 268 reputationPosted Jun 2, 2023, 2:17 AM EDT
Hi Naimish Sir, will it work in my project (web application)