HTML and CSS Reference
GetDistance() helper method determines the distance between the user's location ( lat1 , long2 ) and the
job location ( lat2 , long2 ). If the distance returned by GetDistance() is less than or equal to the distance
specified by the user, the job posting is added to a generic List of Job objects. Before the filtered job
postings are sent to the user, the selectedJobs generic List is sorted on LocationName . Finally, the sorted
list of Job objects is sent to the caller in JSON format using the Json() method. The GetDistance() method
that determines the distance between the user's location and the job location is shown in Listing 12-11.
Listing 12-11. Finding the Distance Between the User's Location and the Job Location
private double GetDistance(double lat1,double long1,double lat2,double long2)
GeoCoordinate point1 = new GeoCoordinate(lat1, long1);
GeoCoordinate point2 = new GeoCoordinate(lat2, long2);
double distance = point1.GetDistanceTo(point2);
This code uses the GeoCoordinate class from the System.Device.Location namespace (located in the
System.Device.dll assembly). GeoCoordinate s represents the geographical coordinates of a location. The
GeoCoordinate constructor accepts a location's latitude and longitude. The GetDistance() method of the
GeoCoordinate class returns the distance between that point and another point as specified in the
parameter. The distance, in meters, is then returned to the caller.
Note that for the sake of simplicity, this code retrieves all the rows from the table and then iterates
through them one by one. A more sophisticated solution would calculate the maximum latitude and
longitude values and then retrieve only rows that fall in that range.
n Note The .NET Framework's GeoCoordinate class is similar to the Geolocation API's coords object in terms
of its properties. GeoCoordinate uses the haversine formula to calculate the distance. This formula treats Earth as
spherical rather than an ellipsoid and doesn't use altitude for distance calculation. The haversine formula introduces
an error of less than 0.1 percent while calculating long distances.
Tracking Movement Using the Geolocation API
In the preceding examples, you used the geolocation object's getCurrentPosition() method to get the
user's current location. This works well when you want to know the user's location at the time you call the
method and you aren't interested in continuously tracking the user. In some cases, however, you need to
keep watching the user's location as the user moves from one place to the other. For example, you may
want to monitor the distance travelled by a user who is moving from a location toward some other
location, and you may want to inform the user regularly about the distance remaining to the target
location. You can use the watchPosition() method of the geolocation object in such cases. This method is
similar to getCurrentPosition() in terms of syntax; but unlike getCurrentPosition() , it keeps invoking the
success function at regular intervals. The exact interval between successive calls to the success function is
governed by the device. The success callback function is invoked only if the user's location has changed.
So, on a desktop computer, getCurrentPosition() and watchPosition() behave in the same fashion
because the location of the computer doesn't change.
What if, based on a condition, you wish to stop watching the user's location? The watchPosition()
method returns a number that acts like a handle to that invocation of watchPosition() . You can pass this