Skip to main content
HTML5 Geolocation API lets you share your location with your favorite web sites. A Javascript can capture your latitude and longitude and can be sent to backend web server and do fancy location-aware things like finding local businesses or showing your location on a map.
Today most of the browsers and mobile devices support Geolocation API. The geolocation APIs work with a new property of the global navigator object ie. Geolocation object which can be created as follows:
var geolocation = navigator.geolocation;
The geolocation object is a service object that allows widgets to retrieve information about the geographic location of the device.

Geolocation Methods

The geolocation object provides the following methods −
MethodDescription
getCurrentPosition()
This method retrieves the current geographic location of the user.
watchPosition()
This method retrieves periodic updates about the current geographic location of the device.
clearWatch()
This method cancels an ongoing watchPosition call.

Example

Following is a sample code to use any of the above method −
function getLocation() {
   var geolocation = navigator.geolocation;
   geolocation.getCurrentPosition(showLocation, errorHandler);
}
Here showLocation and errorHandler are callback methods which would be used to get actual position as explained in next section and to handle errors if there is any.

Location Properties

Geolocation methods getCurrentPosition() and getPositionUsingMethodName() specify the callback method that retrieves the location information. These methods are called asynchronously with an object Position which stores the complete location information.
The Position object specifies the current geographic location of the device. The location is expressed as a set of geographic coordinates together with information about heading and speed.
The following table describes the properties of the Position object. For the optional properties if the system cannot provide a value, the value of the property is set to null.
PropertyTypeDescription
coordsobjects
Specifies the geographic location of the device. The location is expressed as a set of geographic coordinates together with information about heading and speed.
coords.latitudeNumber
Specifies the latitude estimate in decimal degrees. The value range is [-90.00, +90.00].
coords.longitudeNumber
Specifies the longitude estimate in decimal degrees. The value range is [-180.00, +180.00].
coords.altitudeNumber
[Optional] Specifies the altitude estimate in meters above the WGS 84 ellipsoid.
coords.accuracyNumber
[Optional] Specifies the accuracy of the latitude and longitude estimates in meters.
coords.altitudeAccuracyNumber
[Optional] Specifies the accuracy of the altitude estimate in meters.
coords.headingNumber
[Optional] Specifies the device's current direction of movement in degrees counting clockwise relative to true north.
coords.speedNumber
[Optional] Specifies the device's current ground speed in meters per second.
timestampdate
Specifies the time when the location information was retrieved and the Position object created.

Example

Following is a sample code which makes use of Position object. Here showLocation method is a callback method −
function showLocation( position ) {
   var latitude = position.coords.latitude;
   var longitude = position.coords.longitude;
   ...
}

Handling Errors

Geolocation is complicated, and it is very much required to catch any error and handle it gracefully.
The geolocations methods getCurrentPosition() and watchPosition() make use of an error handler callback method which gives PositionError object. This object has following two properties −
PropertyTypeDescription
codeNumber
Contains a numeric code for the error.
messageString
Contains a human-readable description of the error.
The following table describes the possible error codes returned in the PositionError object.
CodeConstantDescription
0UNKNOWN_ERROR
The method failed to retrieve the location of the device due to an unknown error.
1PERMISSION_DENIED
The method failed to retrieve the location of the device because the application does not have permission to use the Location Service.
2POSITION_UNAVAILABLE
The location of the device could not be determined.
3TIMEOUT
The method was unable to retrieve the location information within the specified maximum timeout interval.

Example

Following is a sample code which makes use of PositionError object. Here errorHandler method is a callback method −
function errorHandler( err ) {
   if (err.code == 1) {
      // access is denied
   }
   ...
}

Position Options

Following is the actual syntax of getCurrentPosition() method −
getCurrentPosition(callback, ErrorCallback, options)
Here third argument is the PositionOptions object which specifies a set of options for retrieving the geographic location of the device.
Following are the options which can be specified as third argument −
PropertyTypeDescription
enableHighAccuracyBoolean
Specifies whether the widget wants to receive the most accurate location estimate possible. By default this is false.
timeoutNumber
The timeout property is the number of milliseconds your web application is willing to wait for a position.
maximumAgeNumber
Specifies the expiry time in milliseconds for cached location information.

Example

Following is a sample code which shows how to use above mentioned methods −
function getLocation() {
   var geolocation = navigator.geolocation;
   geolocation.getCurrentPosition(showLocation, errorHandler, {maximumAge: 75000});
}

Comments

Popular posts from this blog

The Windows Firewall with Advanced Security is a firewall that runs on the Windows Server 2012 and is turned on by default. The Firewall settings within Windows Server 2012 are managed from within the  Windows Firewall Microsoft Management Console . To set Firewall settings perform the following steps − Step 1  − Click on the Server Manager from the task bar → Click the Tools menu and select Windows Firewall with Advanced Security. Step 2  − To see the current configuration settings by selecting  Windows Firewall Properties  from the MMC. This  allows access to modify the settings  for each of the three firewall profiles, which are –  Domain, Private and Public  and IPsec settings. Step 3  − Applying custom rules, which will include the following two steps − Select either  Inbound Rules  or  Outbound Rules  under  Windows Firewall with Advanced Security  on the left side of the management console...
In this chapter, we will see how to enable remote desktop application. It is important because this enables us to work remotely on the server. To do this, we have the following two options. For the first option, we have to follow the steps given below. Step 1  − Go to Start → right click “This PC” → Properties. Step 2  − On Left side click “Remote Setting”. Step 3  − Check radio button “Allow Remote connection to this computer” and Check box “Allow connection only from computers running Remote Desktop with Network Level Authentication (recommended)” → click “Select Users”. Step 4  − Click Add. Step 5  − Type user that you want to allow access. In my case, it is administrator → click OK. For the  second option , we need to follow the steps given below. Step 1  − Click on “Server Manage” → Local Server → click on “Enable” or Disable, if it is Disabled.
In this chapter, we will see how to configure WSUS and tune it. The following steps should be followed for configuring it. Step 1  − When you open it for the first time, you should do it by going to “Server Manager” → Tools → Windows Server Update Services, then a Configuration wizard will be opened and then click → Next. Step 2  − Click “Start Connecting” → Wait until the green bar is full and then → Next. Step 3  − Check the box for which the updates want to be taken, I did for English and then → Next. Step 4  − Check the box for all the products which you want to update. It is just for Microsoft products and it is recommended to include all the products related to Microsoft and then → Next. Step 5  − Choose the classification updated to be downloaded, if you have a very good internet speed, then check all the boxes, otherwise just check “Critical Updates”. Step 6  − Now we should schedule the updates which I will recommend to do it a...