HTML Geolocation API

Using HTML Geolocation API

The Geolocation API is accessed via a call to navigator.geolocation. This will cause the browser to ask the user for permission to access their location data. If the user accept, the browser will search for the best available functionality on the device to access this information (for example GPS).

The getCurrentPosition() method is used to return the user’s current location.

The example below returns the latitude and longitude of the user’s current location:

Locate the User’s Position

The Geolocation API is used to access the user’s current location.

Since this can compromise privacy, the location is not available unless the user approves it.

Example

Result

HTML Geolocation

Click the button to get your coordinates.

 


Error Handling and Rejections

The second parameter of the getCurrentPosition() method is used to handle errors. It specifies a function to run if it fails to get the user’s location.

Here is an example of a more specific error handling:

Example

Result

HTML Geolocation

Click the button to get your coordinates.

 

Geolocation Object – Other interesting Methods

The Geolocation object also has other interesting methods:

The example below shows the watchPosition() method. You need an accurate GPS device to test this (like a smartphone):

Example

Result

HTML Geolocation

Click the button to get your coordinates.

 

The getCurrentPosition() Method – Return Data

The getCurrentPosition() method returns an object on success. The latitude, longitude and accuracy properties are always returned. The other properties are returned if available:

Property Returns
coords.latitude The latitude as a decimal number (always returned)
coords.longitude The longitude as a decimal number (always returned)
coords.accuracy The accuracy of position (always returned)
coords.altitude The altitude in meters above the mean sea level (returned if available)
coords.altitudeAccuracy The altitude accuracy of position (returned if available)
coords.heading The heading as degrees clockwise from North (returned if available)
coords.speed The speed in meters per second (returned if available)
timestamp The date/time of the response (returned if available)

 

 

HTML Drag and Drop API

Drag and Drop

Drag and drop is a very common feature. It is when you “grab” an object and drag it to a different location.

Example

Result

 

Make an Element Draggable

First of all: To make an element draggable, set the draggable attribute to true:

or

 

What to Drag – ondragstart and setData()

Then, specify what should happen when the element is dragged.

In the example above, the ondragstart attribute of the <img> element calls a function (dragstartHandler(ev)), that specifies what data to be dragged.

The dataTransfer.setData() method sets the data type and the value of the dragged data:

function dragstartHandler(ev) {
ev.dataTransfer.setData(“text”, ev.target.id);
}

Where to Drop – ondragover

The ondragover attrribute of the <div> element calls a function (dragoverHandler(ev)), that specifies where the dragged data can be dropped.

By default, data/elements cannot be dropped in other elements. To allow a drop, we must prevent the default handling of the element.

This is done by calling the preventDefault() method for the ondragover event:

function dragoverHandler(ev) {
ev.preventDefault();
}

 

 

Window localStorage

Set and retrieve localStorage name/value pair:

Example

Result

The Window Object

The localStorage Property

Saved name is:

Smith

Description

The localStorage object allows you to save key/value pairs in the browser.

Syntax

window.localStorage

or just:

localStorage

Save Data to Local Storage

localStorage.setItem(keyvalue);

Read Data from Local Storage

let lastname = localStorage.getItem(key);

Remove Data from Local Storage

localStorage.removeItem(key);

Remove All (Clear Local Storage)

localStorage.clear();