Geolocation in HTML5

Geolocation | Basics

In general Geolocation is an Application Program Interface (API) of HTML5 that allows us to get current position or location. To provide this important functionality HTML5 uses functions and some concepts of JavaScript.

Use of JavaScript

JavaScript is used to get these basic details to search your base location. Through JavaScript we will get:

  • Latitude
  • Height
  • Longitude
  • Altitude
  • Heading
  • Speed
  • Timestamp
  • Area
  • Region boundary
  • Accuracy

These details will be obtained to enable the main stream code to access or get location information.

Geolocation | Scope

Nowadays, nearly all browsers and mobile devices support this API. To work with location information or those basic details, what we really need is only a supporting device or frame.

Geolocation | Concept

We will use the Geolocation API directly. The major idea or concept behind Geolocation is the creation of an instance of an object. (Generally all programmers are familiar with this concept of creating an object instance.)

Object Creation:

For example: var = navigator.geolocation Geolocation,

Geolocation |Methods

The following are the 3 main methods for creating Geolocation:

  1. getCurrentPosition()

    This method returns the user location.
  2. watchPosition()

    This method returns in the interval position of the device.
  3. clearWatch()

    This method is for canceling an operation.

    Geolocation | Attributes

The following is the most used and important attributesa for Geolocation purposes:

  1. coords: This attribute gathers the information about the location.
                  - Heading and speed.

  2. cords.latitude: This attributes measures latitude in decimal systems.

  3. cords.longitude: This measures geographical longitude.
                                 - Unit: decimal systems

  4. cords.altitude: this attribute measures the altitude in meters.

  5. cords.accuracy: Accuracy of the altitude.
                               - Height
                               - Unit: meters

  6. cords.altitudeAccuracy: This attribute defines the accuracy of the altitude.
                                             - Unit: meters

  7. cords.heading: direction of movement of the device in degrees in a clockwise manner or the direction to the north.

  8. cords.speed: This attributes defines the actual speed of the device.
                          - Speed is measured in mt/sec

  9. timestamp: the time when the location information was obtained as well as the instance of the object created, defined by the attribute TIMESTAMP.

Geolocation | Reference Example

abhishekPolohu function()

Var = navigator.geolocation Geolocation,
Geolocation.getCurrentPosition (savePosition, checkError);


checkError function(error)

   // unknown error

else if(chyba.code==1)
   // access is denied

else if(chyba.code==2)
    // position of the device is unknown

else if(chyba .code==3)
    // time interval has passed
var width = pozicia.coords.latitude;
var height = pozicia.coords.longitude;