HTML and CSS Reference
In-Depth Information
});
});
});
This code shows the Show button's
click
event-handler function. It first retrieves the user's location
using the
getCurrentPosition()
method of the
geolocation
object. This time, a
success
function is
provided inline with the
getCurrentPosition()
call instead of as a separate function. The user's location's
latitude and longitude values are stored in the local variables
lat1
and
long1
. A JSON object with three
keys—
lat1
,
long1
, and
distance
—is formed using the values from the variables and the
<input>
ield.
Next, an Ajax request is made to the
GetJobs()
action method using the jQuery
$.ajax()
method.
GetJobs()
returns zero or more
Job
objects and is discussed shortly. The
success
function of the
$.ajax()
method receives the
Job
objects returned by
GetJobs()
. It then iterates through the jobs array and adds the
jobs to an HTML table. The table contains the
Title
,
Description
, and
LocationName
properties of the
Job
objects.
The error-handler function of the
$.ajax()
method displays the error message in an alert box. The
GetJobs()
action method that returns the relevant jobs based on the user's location and the specified
distance is shown in Listing 12-10.
Listing 12-10.
GetJobs()
Action Method
[HttpPost]
public JsonResult GetJobs(double lat1, double long1, double distance)
{
JobsDbEntities db = new JobsDbEntities();
var data = from item in db.Jobs
select item;
List<Job> selectedJobs = new List<Job>();
foreach(Job job in data)
{
var temp = from item in db.Locations
where item.LocationName==job.LocationName
select item;
double lat2 = (double)((Location)temp.SingleOrDefault()).Latitude;
double long2 = (double)((Location)temp.SingleOrDefault()).Longitude;
if (GetDistance(lat1, long1, lat2, long2) <= distance)
{
selectedJobs.Add(job);
}
}
var finalData = from obj in selectedJobs
orderby obj.LocationName
select obj;
return Json(finalData);
}
The
GetJobs()
action method from the Home controller takes three parameters of type
double
. The
lat1
and
long1
parameters represent the latitude and longitude of the user's location. The
distance
parameter represents the distance specified by the user. The method then iterates through all the available
job postings. With each iteration, the latitude and longitude of the job location are determined. The