четверг, 14 июля 2011 г.

HTML5 Geolocation - standarts in our life.

HTML5 new standart brings about a plenty of interesting and useful possibilities both for web-developers and casual users. One of such features is geolocation API. Geolocation API is an geolocation service interface that user himself can select in browser settings. By default, Google Location Services is used. Service does the following: it returns user’s current location as coordinates(longitude and latitude).

How it works:
Browsers(Firefox, for example): send information on the closest access point to server (if user is connected to any WiFi point). In case user has no access to WiFi browser uses IP address to determine location.
Mobile devices find solution as follows: coordinates are defined by the means of GPS receiver if it is on. Otherwise, mobile devices are allowed to use information on the closest stations.



Security:
Current location of the user is confidential information. Browser asks user for permission before allow application access to this information. This behavior is described clearly in W3C specification here(http://www.w3.org/TR/geolocation-API/#privacy_for_uas). All the data but IP or WiFi points is forbidden to be sent. Thus, cookies and stored passwords stay in safe.

Compatibility:
Currently, geolocation is supported by the browsers from the list below:
IE    Firefox    Safari    Chrome    Opera    iPhone    Android
9.0+    3.5+    5.0+    5.0+    10.6+    3.0+    2.0+

Stick to the point:
Definition of user position is available because of integrated object ‘navigator.geolocation’. More precisely on the example below:

function showMap(position) {
// Show a map centered at 
// (position.coords.latitude, position.coords.longitude).
}

// One-shot position request.
navigator.geolocation.getCurrentPosition(showMap);
getCurrentPosition – uses service to define current user state. If its work is finished successfully callback function will be called and object Position will be passed within.
It is also possible to track user position changes:

function scrollMap(position) {
  // Скроллирует карту так что она бдует центрирована на точку 
(position.coords.latitude, position.coords.longitude).
}

//Стартует слежение за изменениями позиции пользователя
var watchId = navigator.geolocation.watchPosition(scrollMap);

function buttonClickHandler() {
  //Останавливает слежение по клику на кнопке
  navigator.geolocation.clearWatch(watchId);
}
Summary:
Geolocation API is simple and all-in-one solution for multiple tasks.

Комментариев нет:

Отправить комментарий