Get user location with latitude and longitude in Asp.net MVC
///Controller
[HttpGet]
public JsonResult getUserLocation(string location,string latitude,string longitude)
{
try
{
TempData[Constants.Temp_Location] = location;
TempData[Constants.Temp_Location_Lat] = latitude;
TempData[Constants.Temp_Location_Lng] = longitude;
}
catch (Exception ex)
{
LogUtil.Error(ex);
return null;
}
return Json("Success");
}
//call using ajax jquery
$(document).ready(function () {
//get user location
debugger
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(success);
} else {
$("#user-location").val("Your browser doesn\'t support the geolocation api.");
}
});
// function to geocode a lat/long
function getAddress(myLatitude, myLongitude) {
var geocoder = new google.maps.Geocoder(); // create a geocoder object
var location = new google.maps.LatLng(myLatitude, myLongitude); // turn coordinates into an object
geocoder.geocode({ 'latLng': location }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) { // if geocode success
alert(results[0].formatted_address);
$("#user-location").val(results[0].formatted_address); // if address found, pass to processing function
$("#user-location-lat").val(myLatitude);
$("#user-location-lng").val(myLongitude);
$.ajax({
dataType: "json",
url: "/hotel/getUserLocation",
data: { "location": results[0].formatted_address, "latitude": myLatitude, "longitude": myLongitude },
success: function () { }
});
} else {
alert("Geocode failure: " + status); // alert any other error(s)
return false;
}
});
}
function success(position) {
var latitude = position.coords.latitude; // set latitude variable
var longitude = position.coords.longitude; // set longitude variable
getAddress(latitude, longitude);
var address = document.getElementById("user-location")
}
///Controller
[HttpGet]
public JsonResult getUserLocation(string location,string latitude,string longitude)
{
try
{
TempData[Constants.Temp_Location] = location;
TempData[Constants.Temp_Location_Lat] = latitude;
TempData[Constants.Temp_Location_Lng] = longitude;
}
catch (Exception ex)
{
LogUtil.Error(ex);
return null;
}
return Json("Success");
}
//call using ajax jquery
$(document).ready(function () {
//get user location
debugger
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(success);
} else {
$("#user-location").val("Your browser doesn\'t support the geolocation api.");
}
});
// function to geocode a lat/long
function getAddress(myLatitude, myLongitude) {
var geocoder = new google.maps.Geocoder(); // create a geocoder object
var location = new google.maps.LatLng(myLatitude, myLongitude); // turn coordinates into an object
geocoder.geocode({ 'latLng': location }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) { // if geocode success
alert(results[0].formatted_address);
$("#user-location").val(results[0].formatted_address); // if address found, pass to processing function
$("#user-location-lat").val(myLatitude);
$("#user-location-lng").val(myLongitude);
$.ajax({
dataType: "json",
url: "/hotel/getUserLocation",
data: { "location": results[0].formatted_address, "latitude": myLatitude, "longitude": myLongitude },
success: function () { }
});
} else {
alert("Geocode failure: " + status); // alert any other error(s)
return false;
}
});
}
function success(position) {
var latitude = position.coords.latitude; // set latitude variable
var longitude = position.coords.longitude; // set longitude variable
getAddress(latitude, longitude);
var address = document.getElementById("user-location")
}
0 Comments