In my HomeController I have the following method:
public JsonResult AjaxTest(Position postData)
{
Session["lat"] = postData.Lat;
Session["lng"] = postData.Long;
return Json("", JsonRequestBehavior.AllowGet);
}
How can I have lat and lng in my Index method?
It is public async Task<ActionResult> Index()
, if it matters.
The script in the view that is retrieving and passing the user's current coordinates:
var x = document.getElementById("positionButton");
(function getLocation()
{
if (navigator.geolocation)
{
navigator.geolocation.getCurrentPosition(showPosition);
}
}());
function showPosition(position)
{
if (position == null) alert('Position is null');
if (position.coords == null) alert('coords is null');
$('#lat').text(position.coords.latitude);
$('#long').text(position.coords.longitude);
var postData = { Lat: position.coords.latitude, Long: position.coords.longitude };
$.ajax(
{
type: "POST",
contentType: "application/json; charset=utf-8",
url: "@Url.Action("AjaxTest", "Home")",
//dataType: "json",
data: JSON.stringify(postData)
});
}
I need to be able to have the user's current lat/long to get the distance to another position. lat and lng are declared as public variables, so why do they stay 0 when trying to use the coordinates inside the Index method?
Edit, here is the Index method:
public object x;
public async Task<ActionResult> Index()
{
x = Session["lat"];
return View(parkingLot);
}