watchPosition
Summary
Returns a long value that uniquely identifies a watch operation and then asynchronously starts the watch operation.
Method of apis/geolocation/Geolocationapis/geolocation/Geolocation
Syntax
var object = Geolocation.watchPosition(successCallback, errorCallback, options);
Parameters
successCallback
- Data-type
- any
The function to call when geographic position is successfully obtained. The function specified by the successCallback parameter takes one position parameter.
errorCallback
- Data-type
- any
(Optional)
The function to call when the attempt to obtain geographic position fails. The function specified by the errorCallback parameter takes one positionError parameter. To use the options parameter without using the errorCallback parameter, specify a null value for the errorCallback parameter.
options
- Data-type
- any
(Optional)
JSON encoding
Return Value
Returns an object of type unsigned longunsigned long
Type: HRESULT
If this method succeeds, it returns S_OK. Otherwise, it returns an HRESULT error code.
Examples
Obtains users position and position changes for 120s.
var watchID;
var geoLoc;
function showLocation(position)
{
alert('latitude: '+position.coords.latitude+' AND longitude: '+position.coords.longitude);
}
function errorHandler(err) {
if(err.code == 1) {
alert("Error: Access is denied!");
}else if( err.code == 2) {
alert("Error: Position is unavailable!");
}
}
function getLocationUpdate(){
if(navigator.geolocation){
// timeout at 120000 milliseconds (120 seconds)
var options = {timeout:120000};
geoLoc = navigator.geolocation;
watchID = geoLoc.watchPosition(showLocation,
errorHandler,
options);
}else{
alert("Sorry, browser does not support geolocation!");
}
}
getLocationUpdate();
Notes
The function begins acquiring the geographic position and returns immediately. When the position is successfully obtained, the callback function provided in the successCallback parameter is called. The parameter to the successCallback function is a position object that contains the data on the current geographic location. If the attempt to obtain the user’s location fails, the callback function that can be provided as an optional second parameter is called. The error parameter to the errorCallback function is a positionError object that contains an error code indicating the reason for failure.
Note: The first time a document calls the watchPosition function, the client requests permission to access the geographic location of the browser, unless the user has previously chosen to always allow or always deny permission for the website to determine location. If the user denies permission, the function declared by the errorCallback is called and the code attribute of the error parameter of that function is set to PositionError.PERMISSION_DENIED. Support for the attributes of the options parameter depends on the location provider available to the device running the client. There is no guarantee that changing the properties of these attributes will affect the results reported by the location provider. Windows Internet Explorer 9. This property is supported only for webpages displayed in IE9 Standards mode. For more information, please see Defining Document Compatibility.
Related specifications
- W3C Geolocation Specification
- W3C Editor’s Draft
Attributions
This article contains content originally from external sources, including ones licensed under the CC-BY-SA license.
Portions of this content copyright 2012 Mozilla Contributors. This article contains work licensed under the Creative Commons Attribution-Sharealike License v2.5 or later. The original work is available at Mozilla Developer Network: Article
Microsoft Developer Network: Windows Internet Explorer API reference Article