HTML Tutorial | HTML5 API | Geolocation
HTML5 API
All APIs newly added in HTML5 are implemented in JavaScript. Therefore, basic knowledge of JavaScript is required to understand this content.
Geolocation API
The geolocation API is a JavaScript API used to get the user’s current location information. Information about the user’s latitude and longitude is sent to the web server by using JavaScript.
Using this, you can provide location-based services such as showing the user’s location on a map or finding stores near the user. However, because this information has a high potential to infringe on user privacy, it cannot be used without the user’s consent.
The major web browser versions that support the geolocation API are as follows.
| API | ie | chrome | firefox | safari | opera |
|---|---|---|---|---|---|
| geolocation | 9.0 | 5.0 - 49.0 (http) / 50.0 (https) | 3.5 | 5.0 | 16.0 |
Starting with Chrome 50.0, the geolocation API is allowed to work only over secure protocols such as HTTPS.
getCurrentPosition() Method
The getCurrentPosition() method can be used to get the latitude and longitude values of the user’s location. The first argument of this method is a function that outputs the retrieved user location information.
Example
function findLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showYourLocation);
} else {
loc.innerHTML = "This sentence appears when the user's web browser does not support the Geolocation API.";
}
}
The second argument of this method is a function that handles errors related to location information.
Example
function showErrorMsg(error) {
switch(error.code) {
case error.PERMISSION_DENIED:
loc.innerHTML = "This sentence appears when the user denies the request to use the Geolocation API."
break;
case error.POSITION_UNAVAILABLE:
loc.innerHTML = "This sentence appears when the retrieved location information is unavailable."
break;
case error.TIMEOUT:
loc.innerHTML = "This sentence appears when the request to retrieve location information times out."
break;
case error.UNKNOWN_ERROR:
loc.innerHTML = "This sentence appears when an unknown error occurs."
break;
}
}
In this way, the retrieved user location information can be displayed through Google Maps.
Example
function showYourLocation(position) {
var userLat = position.coords.latitude;
var userLng = position.coords.longitude;
var imgUrl = "http://maps.googleapis.com/maps/api/staticmap?center=" + userLat + "," + userLng + "&zoom=15&size=500x400&sensor=false";
document.getElementById("mapLocation").innerHTML = "<img src='"+imgUrl+"'>";
}
Instead of displaying a simple image as in the example above, it is also possible to integrate with the Google Maps script.
Example
function showYourLocation(position) {
var userLat = position.coords.latitude;
var userLng = position.coords.longitude;
var userLocation = new google.maps.LatLng(userLat, userLng);
loc = document.getElementById("mapLocation");
loc.style.width = '500px';
loc.style.height = '400px';
var mapOptions = {
center: userLocation,
zoom: 15,
mapTypeId: google.maps.MapTypeId.ROADMAP,
mapTypeControl: false,
navigationControlOptions: {style: google.maps.NavigationControlStyle.SMALL}
}
var map = new google.maps.Map(loc, mapOptions);
var marker = new google.maps.Marker({position:userLocation,map:map,title:"This is the current location!"});
}
Geolocation API Methods
| Method | Description |
|---|---|
| getCurrentPosition() | Gets the user’s current location. |
| watchPosition() | Gets the user’s current location and then continuously updates location information as the user moves. |
| clearWatch() | Stops execution of the watchPosition() method. |
Return Values of the getCurrentPosition() Method
| Property | Return value |
|---|---|
| coords.latitude | Latitude value expressed as a decimal |
| coords.longitude | Longitude value expressed as a decimal |
| coords.accuracy | Accuracy of the latitude and longitude values |
| coords.altitude | Altitude value relative to mean sea level |
| coords.altitudeAccuracy | Accuracy of the altitude value |
| coords.heading | Clockwise angle value from north for the current direction of travel (degrees) |
| coords.speed | Speed value showing meters moved per second |
| timestamp | Indicates the time when the location information was retrieved. |