Science Knowings: HTML Course For Social Media

HTML Geolocation

HTML Geolocation

HTML Geolocation

In today's session, we'll delve into HTML Geolocation, a powerful API that allows web applications to access the device's location data. Let's get started!

Why Use HTML Geolocation?

HTML Geolocation offers several benefits, including:

  • Location-Based Services: Provide customized experiences based on the user's location (e.g., weather updates, local business listings).
  • Navigation and Tracking: Enable navigation apps, fitness trackers, and other location-aware applications.
  • Security and Fraud Detection: Help detect fraudulent activities by verifying device location.

Getting Started with Geolocation


To access the Geolocation API, use the navigator.geolocation object. It provides methods to determine the device's location.

Geolocation API Methods

  • getCurrentPosition(): Obtains the device's current location coordinates.
  • watchPosition(): Continuously monitors the device's location and returns updates.
  • clearWatch(): Stops the location monitoring initiated by watchPosition().


navigator.geolocation.getCurrentPosition(successCallback, errorCallback);

Use getCurrentPosition() to get a one-time location fix. It takes two callback functions:

  • successCallback: Invoked with a Position object containing the location data.
  • errorCallback: Invoked if an error occurs while obtaining the location.


navigator.geolocation.watchPosition(successCallback, errorCallback);

Use watchPosition() to continuously monitor the device's location. It takes two callback functions, similar to getCurrentPosition().



Use clearWatch() to stop the location monitoring started by watchPosition(). It takes a watchId parameter, which is returned by watchPosition().

Navigator.geolocation Object

The navigator.geolocation object provides additional properties:

  • geolocation.getCurrentPosition: Alias for getCurrentPosition() method.
  • geolocation.watchPosition: Alias for watchPosition() method.
  • geolocation.clearWatch: Alias for clearWatch() method.

Position Object

The Position object contains the location data obtained from getCurrentPosition() or watchPosition(). It has the following properties:

  • coords: Contains latitude, longitude, and accuracy.
  • timestamp: Represents the timestamp when the position was determined.

Geolocation Errors

Geolocation API can encounter various errors. They are represented by numeric error codes in the PositionError object:

  • PERMISSION_DENIED: User denied location access.
  • POSITION_UNAVAILABLE: Location could not be determined.
  • TIMEOUT: Request timed out.

Common Use Cases for Geolocation

  • Location-based recommendations
  • Navigation and mapping
  • Geotagging photos
  • Fraud detection
  • Asset tracking

Best Practices for Geolocation

  • Request user permission for location access.
  • Handle errors gracefully.
  • Consider privacy implications.
  • Use efficient location updates.
  • Document Geolocation API usage in code.

Next Up: HTML Web Storage

HTML Web Storage

In the next session, we'll explore HTML Web Storage, a browser-based API for storing data locally on the client side. Follow our slides to learn more!