Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

1019 lines (854 sloc) 46.725 kB
<!DOCTYPE html>
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
--><html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no, width=device-width">
<meta name="generator" content="joDoc">
<title>Apache Cordova API Documentation</title>
<link rel="stylesheet" type="text/css" href="index.css">
<link rel="stylesheet" type="text/css" href="mobile.css" media="only screen and (max-device-width: 1024px)">
<link rel="stylesheet" type="text/css" href="prettify/prettify.css">
</head>
<body>
<div id="header">
<h1><a href="index.html">Apache <strong>Cordova</strong> Documentation</a></h1>
<small>
<select><optgroup label="English" value="en">
<option value="edge">edge</option>
<option value="2.1.0rc2">2.1.0rc2</option>
<option value="2.1.0rc1">2.1.0rc1</option>
<option selected value="2.1.0">2.1.0</option>
<option value="2.0.0rc1">2.0.0rc1</option>
<option value="2.0.0">2.0.0</option>
<option value="1.9.0rc1">1.9.0rc1</option>
<option value="1.9.0">1.9.0</option>
<option value="1.8.1">1.8.1</option>
<option value="1.8.0rc1">1.8.0rc1</option>
<option value="1.8.0">1.8.0</option>
<option value="1.7.0rc1">1.7.0rc1</option>
<option value="1.7.0">1.7.0</option>
<option value="1.6.1">1.6.1</option>
<option value="1.6.0rc1">1.6.0rc1</option>
<option value="1.6.0">1.6.0</option>
<option value="1.5.0rc1">1.5.0rc1</option>
<option value="1.5.0">1.5.0</option>
<option value="1.4.1">1.4.1</option>
<option value="1.4.0rc1">1.4.0rc1</option>
<option value="1.4.0">1.4.0</option>
<option value="1.3.0">1.3.0</option>
<option value="1.2.0">1.2.0</option>
<option value="1.1.0">1.1.0</option>
<option value="1.0.0rc3">1.0.0rc3</option>
<option value="1.0.0rc2">1.0.0rc2</option>
<option value="1.0.0rc1">1.0.0rc1</option>
<option value="1.0.0">1.0.0</option>
<option value="0.9.6">0.9.6</option>
<option value="0.9.5.1">0.9.5.1</option>
<option value="0.9.5">0.9.5</option>
<option value="0.9.4">0.9.4</option>
<option value="0.9.3">0.9.3</option>
<option value="0.9.2">0.9.2</option>
</optgroup>
<optgroup label="Español" value="es"><option value="1.0.0">1.0.0</option></optgroup>
<optgroup label="Français" value="fr">
<option value="1.3.0">1.3.0</option>
<option value="1.2.0">1.2.0</option>
<option value="1.1.0">1.1.0</option>
</optgroup>
<optgroup label="Japanese" value="jp">
<option value="2.0.0">2.0.0</option>
<option value="1.9.0">1.9.0</option>
<option value="1.8.1">1.8.1</option>
<option value="1.7.0">1.7.0</option>
<option value="0.9.5">0.9.5</option>
</optgroup>
<optgroup label="Korean" value="kr"><option value="2.0.0">2.0.0</option></optgroup></select></small>
</div>
<div id="subheader">
<h1>Geolocation</h1>
<small><select><option value="Geolocation">Geolocation</option>
<option value="Geolocation_methods">      - Methods</option>
<option value="Geolocation_arguments">      - Arguments</option>
<option value="Geolocation_objects_read_only">      - Objects (Read-Only)</option>
<option value="Geolocation_permissions">      - Permissions</option>
<option value="geolocation.getCurrentPosition">geolocation.getCurrentPosition</option>
<option value="geolocation.getCurrentPosition_parameters">      - Parameters</option>
<option value="geolocation.getCurrentPosition_description">      - Description</option>
<option value="geolocation.getCurrentPosition_supported_platforms">      - Supported Platforms</option>
<option value="geolocation.getCurrentPosition_quick_example">      - Quick Example</option>
<option value="geolocation.getCurrentPosition_full_example">      - Full Example</option>
<option value="geolocation.watchPosition">geolocation.watchPosition</option>
<option value="geolocation.watchPosition_parameters">      - Parameters</option>
<option value="geolocation.watchPosition_returns">      - Returns</option>
<option value="geolocation.watchPosition_description">      - Description</option>
<option value="geolocation.watchPosition_supported_platforms">      - Supported Platforms</option>
<option value="geolocation.watchPosition_quick_example">      - Quick Example</option>
<option value="geolocation.watchPosition_full_example">      - Full Example</option>
<option value="geolocation.clearWatch">geolocation.clearWatch</option>
<option value="geolocation.clearWatch_parameters">      - Parameters</option>
<option value="geolocation.clearWatch_description">      - Description</option>
<option value="geolocation.clearWatch_supported_platforms">      - Supported Platforms</option>
<option value="geolocation.clearWatch_quick_example">      - Quick Example</option>
<option value="geolocation.clearWatch_full_example">      - Full Example</option>
<option value="Coordinates">Coordinates</option>
<option value="Coordinates_properties">      - Properties</option>
<option value="Coordinates_description">      - Description</option>
<option value="Coordinates_supported_platforms">      - Supported Platforms</option>
<option value="Coordinates_quick_example">      - Quick Example</option>
<option value="Coordinates_full_example">      - Full Example</option>
<option value="Coordinates_android_quirks">      - Android Quirks</option>
<option value="Position">Position</option>
<option value="Position_properties">      - Properties</option>
<option value="Position_description">      - Description</option>
<option value="Position_supported_platforms">      - Supported Platforms</option>
<option value="Position_quick_example">      - Quick Example</option>
<option value="Position_full_example">      - Full Example</option>
<option value="PositionError">PositionError</option>
<option value="PositionError_properties">      - Properties</option>
<option value="PositionError_constants">      - Constants</option>
<option value="PositionError_description">      - Description</option>
<option value="geolocationSuccess">geolocationSuccess</option>
<option value="geolocationSuccess_parameters">      - Parameters</option>
<option value="geolocationSuccess_example">      - Example</option>
<option value="geolocationError">geolocationError</option>
<option value="geolocationError_parameters">      - Parameters</option>
<option value="geolocationOptions">geolocationOptions</option>
<option value="geolocationOptions_options">      - Options</option>
<option value="geolocationOptions_android_quirks">      - Android Quirks</option></select></small>
</div>
<div id="sidebar">
<div class="vertical_divider"></div>
<h1>API Reference</h1>
<ul>
<li><a href="cordova_accelerometer_accelerometer.md.html#Accelerometer">Accelerometer</a></li>
<li><a href="cordova_camera_camera.md.html#Camera">Camera</a></li>
<li><a href="cordova_media_capture_capture.md.html#Capture">Capture</a></li>
<li><a href="cordova_compass_compass.md.html#Compass">Compass</a></li>
<li><a href="cordova_connection_connection.md.html#Connection">Connection</a></li>
<li><a href="cordova_contacts_contacts.md.html#Contacts">Contacts</a></li>
<li><a href="cordova_device_device.md.html#Device">Device</a></li>
<li><a href="cordova_events_events.md.html#Events">Events</a></li>
<li><a href="cordova_file_file.md.html#File">File</a></li>
<li><a href="cordova_geolocation_geolocation.md.html#Geolocation">Geolocation</a></li>
<li><a href="cordova_media_media.md.html#Media">Media</a></li>
<li><a href="cordova_notification_notification.md.html#Notification">Notification</a></li>
<li><a href="cordova_storage_storage.md.html#Storage">Storage</a></li>
</ul>
<h1>Guides</h1>
<ul>
<li><a href="guide_getting-started_index.md.html#Getting%20Started%20Guides">Getting Started Guides</a></li>
<li><a href="guide_command-line_index.md.html#Command-Line%20Usage">Command-Line Usage</a></li>
<li><a href="guide_upgrading_index.md.html#Upgrading%20Guides">Upgrading Guides</a></li>
<li><a href="guide_plugin-development_index.md.html#Plugin%20Development%20Guide">Plugin Development Guide</a></li>
<li><a href="guide_whitelist_index.md.html#Domain%20Whitelist%20Guide">Domain Whitelist Guide</a></li>
<li><a href="guide_cordova-webview_index.md.html#Embedding%20WebView">Embedding WebView</a></li>
<li><a href="_index.html">Keyword Index</a></li>
</ul>
</div>
<div id="scrollable">
<div id="content">
<h1><a name="Geolocation">Geolocation</a></h1>
<blockquote>
<p>The <code>geolocation</code> object provides access to the device's GPS sensor.</p>
</blockquote>
<p><a href="cordova_geolocation_geolocation.md.html#Geolocation">Geolocation</a> provides location information for the device, such as latitude and longitude. Common sources of location information include Global Positioning System (GPS) and location inferred from network signals such as IP address, RFID, WiFi and Bluetooth MAC addresses, and GSM/CDMA cell IDs. No guarantee is given that the API returns the device's actual location.</p>
<p>This API is based on the <a class="external" href="http://dev.w3.org/geo/api/spec-source.html">W3C </a><a href="cordova_geolocation_geolocation.md.html#Geolocation">Geolocation</a> API Specification. Some devices (Android, BlackBerry, Bada, Windows Phone 7, webOS and Tizen, to be specific) already provide an implementation of this spec. For those devices, the built-in support is used instead of replacing it with Cordova's implementation. For devices that don't have geolocation support, the Cordova implementation adheres to the W3C specification.</p>
<h2>
<a name="Geolocation_methods">Methods</a>
</h2>
<ul>
<li><a href="cordova_geolocation_geolocation.md.html#geolocation.getCurrentPosition">geolocation.getCurrentPosition</a></li>
<li><a href="cordova_geolocation_geolocation.md.html#geolocation.watchPosition">geolocation.watchPosition</a></li>
<li><a href="cordova_geolocation_geolocation.md.html#geolocation.clearWatch">geolocation.clearWatch</a></li>
</ul>
<h2>
<a name="Geolocation_arguments">Arguments</a>
</h2>
<ul>
<li><a href="cordova_geolocation_geolocation.md.html#geolocationSuccess">geolocationSuccess</a></li>
<li><a href="cordova_geolocation_geolocation.md.html#geolocationError">geolocationError</a></li>
<li><a href="cordova_geolocation_geolocation.md.html#geolocationOptions">geolocationOptions</a></li>
</ul>
<h2>
<a name="Geolocation_objects_read_only">Objects (Read-Only)</a>
</h2>
<ul>
<li><a href="cordova_geolocation_geolocation.md.html#Position">Position</a></li>
<li><a href="cordova_geolocation_geolocation.md.html#PositionError">PositionError</a></li>
<li><a href="cordova_geolocation_geolocation.md.html#Coordinates">Coordinates</a></li>
</ul>
<h2>
<a name="Geolocation_permissions">Permissions</a>
</h2>
<h3>Android</h3>
<h4>app/res/xml/plugins.xml</h4>
<pre class="prettyprint"><code>&lt;plugin name="<a href="cordova_geolocation_geolocation.md.html#Geolocation">Geolocation</a>" value="org.apache.cordova.GeoBroker" /&gt;
</code></pre>
<h4>app/AndroidManifest.xml</h4>
<pre class="prettyprint"><code>&lt;uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /&gt;
&lt;uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /&gt;
&lt;uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" /&gt;
</code></pre>
<h3>Bada</h3>
<pre class="prettyprint"><code>No permissions are required.
</code></pre>
<h3>BlackBerry WebWorks</h3>
<h4>www/plugins.xml</h4>
<pre class="prettyprint"><code>&lt;plugin name="<a href="cordova_geolocation_geolocation.md.html#Geolocation">Geolocation</a>" value="org.apache.cordova.geolocation.<a href="cordova_geolocation_geolocation.md.html#Geolocation">Geolocation</a>" /&gt;
</code></pre>
<h4>www/config.xml</h4>
<pre class="prettyprint"><code>&lt;rim:permissions&gt;
&lt;rim:permit&gt;read_geolocation&lt;/rim:permit&gt;
&lt;/rim:permissions&gt;
</code></pre>
<h3>iOS</h3>
<h4>App/Supporting Files/Cordova.plist</h4>
<pre class="prettyprint"><code>&lt;key&gt;Plugins&lt;/key&gt;
&lt;dict&gt;
&lt;key&gt;<a href="cordova_geolocation_geolocation.md.html#Geolocation">Geolocation</a>&lt;/key&gt;
&lt;string&gt;CDVLocation&lt;/string&gt;
&lt;/dict&gt;
</code></pre>
<h3>webOS</h3>
<pre class="prettyprint"><code>No permissions are required.
</code></pre>
<h3>Windows Phone</h3>
<h4>Properties/WPAppManifest.xml</h4>
<pre class="prettyprint"><code>&lt;Capabilities&gt;
&lt;Capability Name="ID_CAP_LOCATION" /&gt;
&lt;/Capabilities&gt;
</code></pre>
<p>Reference: <a class="external" href="http://msdn.microsoft.com/en-us/library/ff769509%28v=vs.92%29.aspx">Application Manifest for Windows Phone</a></p>
<h3>Tizen</h3>
<pre class="prettyprint"><code>No permissions are required.
</code></pre>
<hr>
<h1><a name="geolocation.getCurrentPosition">geolocation.getCurrentPosition</a></h1>
<p>Returns the device's current position as a <code><a href="cordova_geolocation_geolocation.md.html#Position">Position</a></code> object.</p>
<pre class="prettyprint"><code>navigator.<a href="cordova_geolocation_geolocation.md.html#geolocation.getCurrentPosition">geolocation.getCurrentPosition</a>(<a href="cordova_geolocation_geolocation.md.html#geolocationSuccess">geolocationSuccess</a>,
[<a href="cordova_geolocation_geolocation.md.html#geolocationError">geolocationError</a>],
[<a href="cordova_geolocation_geolocation.md.html#geolocationOptions">geolocationOptions</a>]);
</code></pre>
<h2>
<a name="geolocation.getCurrentPosition_parameters">Parameters</a>
</h2>
<ul>
<li>
<strong><a href="cordova_geolocation_geolocation.md.html#geolocationSuccess">geolocationSuccess</a></strong>: The callback that is called with the current position.</li>
<li>
<strong><a href="cordova_geolocation_geolocation.md.html#geolocationError">geolocationError</a></strong>: (Optional) The callback that is called if there was an error.</li>
<li>
<strong><a href="cordova_geolocation_geolocation.md.html#geolocationOptions">geolocationOptions</a></strong>: (Optional) The geolocation options.</li>
</ul>
<h2>
<a name="geolocation.getCurrentPosition_description">Description</a>
</h2>
<p><code>geolocation.getCurrentPositon</code> is an asynchronous function. It returns the device's current position to the <code><a href="cordova_geolocation_geolocation.md.html#geolocationSuccess">geolocationSuccess</a></code> callback with a <code><a href="cordova_geolocation_geolocation.md.html#Position">Position</a></code> object as the parameter. If there is an error, the <code><a href="cordova_geolocation_geolocation.md.html#geolocationError">geolocationError</a></code> callback is invoked with a <code><a href="cordova_geolocation_geolocation.md.html#PositionError">PositionError</a></code> object.</p>
<h2>
<a name="geolocation.getCurrentPosition_supported_platforms">Supported Platforms</a>
</h2>
<ul>
<li>Android</li>
<li>BlackBerry WebWorks (OS 5.0 and higher)</li>
<li>iOS</li>
<li>Windows Phone 7 ( Mango )</li>
<li>Bada 1.2 &amp; 2.x</li>
<li>webOS</li>
<li>Tizen</li>
</ul>
<h2>
<a name="geolocation.getCurrentPosition_quick_example">Quick Example</a>
</h2>
<pre class="prettyprint"><code>// onSuccess Callback
// This method accepts a `<a href="cordova_geolocation_geolocation.md.html#Position">Position</a>` object, which contains
// the current GPS coordinates
//
var onSuccess = function(position) {
alert('Latitude: ' + position.coords.latitude + '\n' +
'Longitude: ' + position.coords.longitude + '\n' +
'Altitude: ' + position.coords.altitude + '\n' +
'Accuracy: ' + position.coords.accuracy + '\n' +
'Altitude Accuracy: ' + position.coords.altitudeAccuracy + '\n' +
'Heading: ' + position.coords.heading + '\n' +
'Speed: ' + position.coords.speed + '\n' +
'Timestamp: ' + position.timestamp + '\n');
};
// onError Callback receives a <a href="cordova_geolocation_geolocation.md.html#PositionError">PositionError</a> object
//
function onError(error) {
alert('code: ' + error.code + '\n' +
'message: ' + error.message + '\n');
}
navigator.<a href="cordova_geolocation_geolocation.md.html#geolocation.getCurrentPosition">geolocation.getCurrentPosition</a>(onSuccess, onError);
</code></pre>
<h2>
<a name="geolocation.getCurrentPosition_full_example">Full Example</a>
</h2>
<pre class="prettyprint"><code>&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;<a href="cordova_device_device.md.html#Device">Device</a> Properties Example&lt;/title&gt;
&lt;script type="text/javascript" charset="utf-8" src="cordova-2.1.0.js"&gt;&lt;/script&gt;
&lt;script type="text/javascript" charset="utf-8"&gt;
// Wait for Cordova to load
//
document.addEventListener("<a href="cordova_events_events.md.html#deviceready">deviceready</a>", onDeviceReady, false);
// Cordova is ready
//
function onDeviceReady() {
navigator.<a href="cordova_geolocation_geolocation.md.html#geolocation.getCurrentPosition">geolocation.getCurrentPosition</a>(onSuccess, onError);
}
// onSuccess <a href="cordova_geolocation_geolocation.md.html#Geolocation">Geolocation</a>
//
function onSuccess(position) {
var element = document.getElementById('geolocation');
element.innerHTML = 'Latitude: ' + position.coords.latitude + '&lt;br /&gt;' +
'Longitude: ' + position.coords.longitude + '&lt;br /&gt;' +
'Altitude: ' + position.coords.altitude + '&lt;br /&gt;' +
'Accuracy: ' + position.coords.accuracy + '&lt;br /&gt;' +
'Altitude Accuracy: ' + position.coords.altitudeAccuracy + '&lt;br /&gt;' +
'Heading: ' + position.coords.heading + '&lt;br /&gt;' +
'Speed: ' + position.coords.speed + '&lt;br /&gt;' +
'Timestamp: ' + position.timestamp + '&lt;br /&gt;';
}
// onError Callback receives a <a href="cordova_geolocation_geolocation.md.html#PositionError">PositionError</a> object
//
function onError(error) {
alert('code: ' + error.code + '\n' +
'message: ' + error.message + '\n');
}
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;p id="geolocation"&gt;Finding geolocation...&lt;/p&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre>
<hr>
<h1><a name="geolocation.watchPosition">geolocation.watchPosition</a></h1>
<p>Watches for changes to the device's current position.</p>
<pre class="prettyprint"><code>var watchId = navigator.<a href="cordova_geolocation_geolocation.md.html#geolocation.watchPosition">geolocation.watchPosition</a>(<a href="cordova_geolocation_geolocation.md.html#geolocationSuccess">geolocationSuccess</a>,
[<a href="cordova_geolocation_geolocation.md.html#geolocationError">geolocationError</a>],
[<a href="cordova_geolocation_geolocation.md.html#geolocationOptions">geolocationOptions</a>]);
</code></pre>
<h2>
<a name="geolocation.watchPosition_parameters">Parameters</a>
</h2>
<ul>
<li>
<strong><a href="cordova_geolocation_geolocation.md.html#geolocationSuccess">geolocationSuccess</a></strong>: The callback that is called with the current position.</li>
<li>
<strong><a href="cordova_geolocation_geolocation.md.html#geolocationError">geolocationError</a></strong>: (Optional) The callback that is called if there was an error.</li>
<li>
<strong><a href="cordova_geolocation_geolocation.md.html#geolocationOptions">geolocationOptions</a></strong>: (Optional) The geolocation options.</li>
</ul>
<h2>
<a name="geolocation.watchPosition_returns">Returns</a>
</h2>
<ul>
<li>
<strong>String</strong>: returns a watch id that references the watch position interval. The watch id should be used with <code><a href="cordova_geolocation_geolocation.md.html#geolocation.clearWatch">geolocation.clearWatch</a></code> to stop watching for changes in position.</li>
</ul>
<h2>
<a name="geolocation.watchPosition_description">Description</a>
</h2>
<p><code><a href="cordova_geolocation_geolocation.md.html#geolocation.watchPosition">geolocation.watchPosition</a></code> is an asynchronous function. It returns the device's current position when a change in position has been detected. When the device has retrieved a new location, the <code><a href="cordova_geolocation_geolocation.md.html#geolocationSuccess">geolocationSuccess</a></code> callback is invoked with a <code><a href="cordova_geolocation_geolocation.md.html#Position">Position</a></code> object as the parameter. If there is an error, the <code><a href="cordova_geolocation_geolocation.md.html#geolocationError">geolocationError</a></code> callback is invoked with a <code><a href="cordova_geolocation_geolocation.md.html#PositionError">PositionError</a></code> object.</p>
<h2>
<a name="geolocation.watchPosition_supported_platforms">Supported Platforms</a>
</h2>
<ul>
<li>Android</li>
<li>BlackBerry WebWorks (OS 5.0 and higher)</li>
<li>iOS</li>
<li>Windows Phone 7 ( Mango )</li>
<li>Bada 1.2 &amp; 2.x</li>
<li>webOS</li>
<li>Tizen</li>
</ul>
<h2>
<a name="geolocation.watchPosition_quick_example">Quick Example</a>
</h2>
<pre class="prettyprint"><code>// onSuccess Callback
// This method accepts a `<a href="cordova_geolocation_geolocation.md.html#Position">Position</a>` object, which contains
// the current GPS coordinates
//
function onSuccess(position) {
var element = document.getElementById('geolocation');
element.innerHTML = 'Latitude: ' + position.coords.latitude + '&lt;br /&gt;' +
'Longitude: ' + position.coords.longitude + '&lt;br /&gt;' +
'&lt;hr /&gt;' + element.innerHTML;
}
// onError Callback receives a <a href="cordova_geolocation_geolocation.md.html#PositionError">PositionError</a> object
//
function onError(error) {
alert('code: ' + error.code + '\n' +
'message: ' + error.message + '\n');
}
// Options: throw an error if no update is received every 30 seconds.
//
var watchID = navigator.<a href="cordova_geolocation_geolocation.md.html#geolocation.watchPosition">geolocation.watchPosition</a>(onSuccess, onError, { timeout: 30000 });
</code></pre>
<h2>
<a name="geolocation.watchPosition_full_example">Full Example</a>
</h2>
<pre class="prettyprint"><code>&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;<a href="cordova_device_device.md.html#Device">Device</a> Properties Example&lt;/title&gt;
&lt;script type="text/javascript" charset="utf-8" src="cordova-2.1.0.js"&gt;&lt;/script&gt;
&lt;script type="text/javascript" charset="utf-8"&gt;
// Wait for Cordova to load
//
document.addEventListener("<a href="cordova_events_events.md.html#deviceready">deviceready</a>", onDeviceReady, false);
var watchID = null;
// Cordova is ready
//
function onDeviceReady() {
// Throw an error if no update is received every 30 seconds
var options = { timeout: 30000 };
watchID = navigator.<a href="cordova_geolocation_geolocation.md.html#geolocation.watchPosition">geolocation.watchPosition</a>(onSuccess, onError, options);
}
// onSuccess <a href="cordova_geolocation_geolocation.md.html#Geolocation">Geolocation</a>
//
function onSuccess(position) {
var element = document.getElementById('geolocation');
element.innerHTML = 'Latitude: ' + position.coords.latitude + '&lt;br /&gt;' +
'Longitude: ' + position.coords.longitude + '&lt;br /&gt;' +
'&lt;hr /&gt;' + element.innerHTML;
}
// onError Callback receives a <a href="cordova_geolocation_geolocation.md.html#PositionError">PositionError</a> object
//
function onError(error) {
alert('code: ' + error.code + '\n' +
'message: ' + error.message + '\n');
}
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;p id="geolocation"&gt;Watching geolocation...&lt;/p&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre>
<hr>
<h1><a name="geolocation.clearWatch">geolocation.clearWatch</a></h1>
<p>Stop watching for changes to the device's location referenced by the <code>watchID</code> parameter.</p>
<pre class="prettyprint"><code>navigator.<a href="cordova_geolocation_geolocation.md.html#geolocation.clearWatch">geolocation.clearWatch</a>(watchID);
</code></pre>
<h2>
<a name="geolocation.clearWatch_parameters">Parameters</a>
</h2>
<ul>
<li>
<strong>watchID:</strong> The id of the <code>watchPosition</code> interval to clear. (String)</li>
</ul>
<h2>
<a name="geolocation.clearWatch_description">Description</a>
</h2>
<p><code><a href="cordova_geolocation_geolocation.md.html#geolocation.clearWatch">geolocation.clearWatch</a></code> stops watching changes to the device's location by clearing the <code><a href="cordova_geolocation_geolocation.md.html#geolocation.watchPosition">geolocation.watchPosition</a></code> referenced by <code>watchID</code>.</p>
<h2>
<a name="geolocation.clearWatch_supported_platforms">Supported Platforms</a>
</h2>
<ul>
<li>Android</li>
<li>BlackBerry WebWorks (OS 5.0 and higher)</li>
<li>iOS</li>
<li>Windows Phone 7 ( Mango )</li>
<li>Bada 1.2 &amp; 2.x</li>
<li>webOS</li>
<li>Tizen</li>
</ul>
<h2>
<a name="geolocation.clearWatch_quick_example">Quick Example</a>
</h2>
<pre class="prettyprint"><code>// Options: watch for changes in position, and use the most
// accurate position acquisition method available.
//
var watchID = navigator.<a href="cordova_geolocation_geolocation.md.html#geolocation.watchPosition">geolocation.watchPosition</a>(onSuccess, onError, { enableHighAccuracy: true });
// ...later on...
navigator.<a href="cordova_geolocation_geolocation.md.html#geolocation.clearWatch">geolocation.clearWatch</a>(watchID);
</code></pre>
<h2>
<a name="geolocation.clearWatch_full_example">Full Example</a>
</h2>
<pre class="prettyprint"><code>&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;<a href="cordova_device_device.md.html#Device">Device</a> Properties Example&lt;/title&gt;
&lt;script type="text/javascript" charset="utf-8" src="cordova-2.1.0.js"&gt;&lt;/script&gt;
&lt;script type="text/javascript" charset="utf-8"&gt;
// Wait for Cordova to load
//
document.addEventListener("<a href="cordova_events_events.md.html#deviceready">deviceready</a>", onDeviceReady, false);
var watchID = null;
// Cordova is ready
//
function onDeviceReady() {
// Get the most accurate position updates available on the
// device.
var options = { enableHighAccuracy: true };
watchID = navigator.<a href="cordova_geolocation_geolocation.md.html#geolocation.watchPosition">geolocation.watchPosition</a>(onSuccess, onError, options);
}
// onSuccess <a href="cordova_geolocation_geolocation.md.html#Geolocation">Geolocation</a>
//
function onSuccess(position) {
var element = document.getElementById('geolocation');
element.innerHTML = 'Latitude: ' + position.coords.latitude + '&lt;br /&gt;' +
'Longitude: ' + position.coords.longitude + '&lt;br /&gt;' +
'&lt;hr /&gt;' + element.innerHTML;
}
// clear the watch that was started earlier
//
function clearWatch() {
if (watchID != null) {
navigator.<a href="cordova_geolocation_geolocation.md.html#geolocation.clearWatch">geolocation.clearWatch</a>(watchID);
watchID = null;
}
}
// onError Callback receives a <a href="cordova_geolocation_geolocation.md.html#PositionError">PositionError</a> object
//
function onError(error) {
alert('code: ' + error.code + '\n' +
'message: ' + error.message + '\n');
}
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;p id="geolocation"&gt;Watching geolocation...&lt;/p&gt;
&lt;button onclick="clearWatch();"&gt;Clear Watch&lt;/button&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre>
<hr>
<h1><a name="Coordinates">Coordinates</a></h1>
<p>A set of properties that describe the geographic coordinates of a position.</p>
<h2>
<a name="Coordinates_properties">Properties</a>
</h2>
<ul>
<li>
<strong>latitude</strong>: Latitude in decimal degrees. <em>(Number)</em>
</li>
<li>
<strong>longitude</strong>: Longitude in decimal degrees. <em>(Number)</em>
</li>
<li>
<strong>altitude</strong>: Height of the position in meters above the ellipsoid. <em>(Number)</em>
</li>
<li>
<strong>accuracy</strong>: Accuracy level of the latitude and longitude coordinates in meters. <em>(Number)</em>
</li>
<li>
<strong>altitudeAccuracy</strong>: Accuracy level of the altitude coordinate in meters. <em>(Number)</em>
</li>
<li>
<strong>heading</strong>: Direction of travel, specified in degrees counting clockwise relative to the true north. <em>(Number)</em>
</li>
<li>
<strong>speed</strong>: Current ground speed of the device, specified in meters per second. <em>(Number)</em>
</li>
</ul>
<h2>
<a name="Coordinates_description">Description</a>
</h2>
<p>The <code><a href="cordova_geolocation_geolocation.md.html#Coordinates">Coordinates</a></code> object is created and populated by Cordova, and attached to the <code><a href="cordova_geolocation_geolocation.md.html#Position">Position</a></code> object. The <code><a href="cordova_geolocation_geolocation.md.html#Position">Position</a></code> object is then returned to the user through a callback function.</p>
<h2>
<a name="Coordinates_supported_platforms">Supported Platforms</a>
</h2>
<ul>
<li>Android</li>
<li>BlackBerry WebWorks (OS 5.0 and higher)</li>
<li>iOS</li>
<li>Windows Phone 7 ( Mango )</li>
<li>Bada 1.2 &amp; 2.x</li>
<li>webOS</li>
<li>Tizen</li>
</ul>
<h2>
<a name="Coordinates_quick_example">Quick Example</a>
</h2>
<pre class="prettyprint"><code>// onSuccess Callback
//
var onSuccess = function(position) {
alert('Latitude: ' + position.coords.latitude + '\n' +
'Longitude: ' + position.coords.longitude + '\n' +
'Altitude: ' + position.coords.altitude + '\n' +
'Accuracy: ' + position.coords.accuracy + '\n' +
'Altitude Accuracy: ' + position.coords.altitudeAccuracy + '\n' +
'Heading: ' + position.coords.heading + '\n' +
'Speed: ' + position.coords.speed + '\n' +
'Timestamp: ' + position.timestamp + '\n');
};
// onError Callback
//
var onError = function() {
alert('onError!');
};
navigator.<a href="cordova_geolocation_geolocation.md.html#geolocation.getCurrentPosition">geolocation.getCurrentPosition</a>(onSuccess, onError);
</code></pre>
<h2>
<a name="Coordinates_full_example">Full Example</a>
</h2>
<pre class="prettyprint"><code>&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;<a href="cordova_geolocation_geolocation.md.html#Geolocation">Geolocation</a> <a href="cordova_geolocation_geolocation.md.html#Position">Position</a> Example&lt;/title&gt;
&lt;script type="text/javascript" charset="utf-8" src="cordova-2.1.0.js"&gt;&lt;/script&gt;
&lt;script type="text/javascript" charset="utf-8"&gt;
// Set an event to wait for Cordova to load
//
document.addEventListener("<a href="cordova_events_events.md.html#deviceready">deviceready</a>", onDeviceReady, false);
// Cordova is loaded and Ready
//
function onDeviceReady() {
navigator.<a href="cordova_geolocation_geolocation.md.html#geolocation.getCurrentPosition">geolocation.getCurrentPosition</a>(onSuccess, onError);
}
// Display `<a href="cordova_geolocation_geolocation.md.html#Position">Position</a>` properties from the geolocation
//
function onSuccess(position) {
var div = document.getElementById('myDiv');
div.innerHTML = 'Latitude: ' + position.coords.latitude + '&lt;br/&gt;' +
'Longitude: ' + position.coords.longitude + '&lt;br/&gt;' +
'Altitude: ' + position.coords.altitude + '&lt;br/&gt;' +
'Accuracy: ' + position.coords.accuracy + '&lt;br/&gt;' +
'Altitude Accuracy: ' + position.coords.altitudeAccuracy + '&lt;br/&gt;' +
'Heading: ' + position.coords.heading + '&lt;br/&gt;' +
'Speed: ' + position.coords.speed + '&lt;br/&gt;';
}
// Show an alert if there is a problem getting the geolocation
//
function onError() {
alert('onError!');
}
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div id="myDiv"&gt;&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre>
<h2>
<a name="Coordinates_android_quirks">Android Quirks</a>
</h2>
<p><strong>altitudeAccuracy:</strong> This property is not support by Android devices, it will always return null.</p>
<hr>
<h1><a name="Position">Position</a></h1>
<p>Contains <code><a href="cordova_geolocation_geolocation.md.html#Position">Position</a></code> coordinates and timestamp, created by the geolocation API.</p>
<h2>
<a name="Position_properties">Properties</a>
</h2>
<ul>
<li>
<strong>coords:</strong> A set of geographic coordinates. <em>(<a href="cordova_geolocation_geolocation.md.html#Coordinates">Coordinates</a>)</em>
</li>
<li>
<strong>timestamp:</strong> Creation timestamp for <code>coords</code>. <em>(Date)</em>
</li>
</ul>
<h2>
<a name="Position_description">Description</a>
</h2>
<p>The <code><a href="cordova_geolocation_geolocation.md.html#Position">Position</a></code> object is created and populated by Cordova, and returned to the user through a callback function.</p>
<h2>
<a name="Position_supported_platforms">Supported Platforms</a>
</h2>
<ul>
<li>Android</li>
<li>BlackBerry WebWorks (OS 5.0 and higher)</li>
<li>iOS</li>
<li>Windows Phone 7 ( Mango )</li>
<li>Bada 1.2 &amp; 2.x</li>
<li>webOS</li>
<li>Tizen</li>
</ul>
<h2>
<a name="Position_quick_example">Quick Example</a>
</h2>
<pre class="prettyprint"><code>// onSuccess Callback
//
var onSuccess = function(position) {
alert('Latitude: ' + position.coords.latitude + '\n' +
'Longitude: ' + position.coords.longitude + '\n' +
'Altitude: ' + position.coords.altitude + '\n' +
'Accuracy: ' + position.coords.accuracy + '\n' +
'Altitude Accuracy: ' + position.coords.altitudeAccuracy + '\n' +
'Heading: ' + position.coords.heading + '\n' +
'Speed: ' + position.coords.speed + '\n' +
'Timestamp: ' + position.timestamp + '\n');
};
// onError Callback receives a <a href="cordova_geolocation_geolocation.md.html#PositionError">PositionError</a> object
//
function onError(error) {
alert('code: ' + error.code + '\n' +
'message: ' + error.message + '\n');
}
navigator.<a href="cordova_geolocation_geolocation.md.html#geolocation.getCurrentPosition">geolocation.getCurrentPosition</a>(onSuccess, onError);
</code></pre>
<h2>
<a name="Position_full_example">Full Example</a>
</h2>
<pre class="prettyprint"><code>&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;<a href="cordova_device_device.md.html#Device">Device</a> Properties Example&lt;/title&gt;
&lt;script type="text/javascript" charset="utf-8" src="cordova-2.1.0.js"&gt;&lt;/script&gt;
&lt;script type="text/javascript" charset="utf-8"&gt;
// Wait for Cordova to load
//
document.addEventListener("<a href="cordova_events_events.md.html#deviceready">deviceready</a>", onDeviceReady, false);
// Cordova is ready
//
function onDeviceReady() {
navigator.<a href="cordova_geolocation_geolocation.md.html#geolocation.getCurrentPosition">geolocation.getCurrentPosition</a>(onSuccess, onError);
}
// onSuccess <a href="cordova_geolocation_geolocation.md.html#Geolocation">Geolocation</a>
//
function onSuccess(position) {
var element = document.getElementById('geolocation');
element.innerHTML = 'Latitude: ' + position.coords.latitude + '&lt;br /&gt;' +
'Longitude: ' + position.coords.longitude + '&lt;br /&gt;' +
'Altitude: ' + position.coords.altitude + '&lt;br /&gt;' +
'Accuracy: ' + position.coords.accuracy + '&lt;br /&gt;' +
'Altitude Accuracy: ' + position.coords.altitudeAccuracy + '&lt;br /&gt;' +
'Heading: ' + position.coords.heading + '&lt;br /&gt;' +
'Speed: ' + position.coords.speed + '&lt;br /&gt;' +
'Timestamp: ' +
position.timestamp + '&lt;br /&gt;';
}
// onError Callback receives a <a href="cordova_geolocation_geolocation.md.html#PositionError">PositionError</a> object
//
function onError(error) {
alert('code: ' + error.code + '\n' +
'message: ' + error.message + '\n');
}
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;p id="geolocation"&gt;Finding geolocation...&lt;/p&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre>
<hr>
<h1><a name="PositionError">PositionError</a></h1>
<p>A <code><a href="cordova_geolocation_geolocation.md.html#PositionError">PositionError</a></code> object is returned to the <code><a href="cordova_geolocation_geolocation.md.html#geolocationError">geolocationError</a></code> callback when an error occurs.</p>
<h2>
<a name="PositionError_properties">Properties</a>
</h2>
<ul>
<li>
<strong>code:</strong> One of the predefined error codes listed below.</li>
<li>
<strong>message:</strong> Error message describing the details of the error encountered.</li>
</ul>
<h2>
<a name="PositionError_constants">Constants</a>
</h2>
<ul>
<li><code><a href="cordova_geolocation_geolocation.md.html#PositionError">PositionError</a>.PERMISSION_DENIED</code></li>
<li><code><a href="cordova_geolocation_geolocation.md.html#PositionError">PositionError</a>.POSITION_UNAVAILABLE</code></li>
<li><code><a href="cordova_geolocation_geolocation.md.html#PositionError">PositionError</a>.TIMEOUT</code></li>
</ul>
<h2>
<a name="PositionError_description">Description</a>
</h2>
<p>The <code><a href="cordova_geolocation_geolocation.md.html#PositionError">PositionError</a></code> object is returned to the user through the <code><a href="cordova_geolocation_geolocation.md.html#geolocationError">geolocationError</a></code> callback function when an error occurs with geolocation.</p>
<h3><code><a href="cordova_geolocation_geolocation.md.html#PositionError">PositionError</a>.PERMISSION_DENIED</code></h3>
<p>Returned when the user does not allow your application to retrieve
position information. This is dependent on the platform.</p>
<h3><code><a href="cordova_geolocation_geolocation.md.html#PositionError">PositionError</a>.POSITION_UNAVAILABLE</code></h3>
<p>Returned when the device was unable to retrieve a position. In general
this means the device has no network connectivity and/or cannot get a
satellite fix.</p>
<h3><code><a href="cordova_geolocation_geolocation.md.html#PositionError">PositionError</a>.TIMEOUT</code></h3>
<p>Returned when the device was unable to retrieve a position within the
time specified in the <code><a href="cordova_geolocation_geolocation.md.html#geolocationOptions">geolocationOptions</a></code>' <code>timeout</code> property. When using
in conjunction with <code><a href="cordova_geolocation_geolocation.md.html#geolocation.watchPosition">geolocation.watchPosition</a></code>, this error could be
called into the <code><a href="cordova_geolocation_geolocation.md.html#geolocationError">geolocationError</a></code> callback every <code>timeout</code> milliseconds.</p>
<hr>
<h1><a name="geolocationSuccess">geolocationSuccess</a></h1>
<p>The user's callback function that is called when a geolocation position becomes available (when using with <code><a href="cordova_geolocation_geolocation.md.html#geolocation.getCurrentPosition">geolocation.getCurrentPosition</a></code>), or when the position changes (when using with <code><a href="cordova_geolocation_geolocation.md.html#geolocation.watchPosition">geolocation.watchPosition</a></code>).</p>
<pre class="prettyprint"><code>function(position) {
// Do something
}
</code></pre>
<h2>
<a name="geolocationSuccess_parameters">Parameters</a>
</h2>
<ul>
<li>
<strong>position:</strong> The geolocation position returned by the device. (<code><a href="cordova_geolocation_geolocation.md.html#Position">Position</a></code>)</li>
</ul>
<h2>
<a name="geolocationSuccess_example">Example</a>
</h2>
<pre class="prettyprint"><code>function <a href="cordova_geolocation_geolocation.md.html#geolocationSuccess">geolocationSuccess</a>(position) {
alert('Latitude: ' + position.coords.latitude + '\n' +
'Longitude: ' + position.coords.longitude + '\n' +
'Altitude: ' + position.coords.altitude + '\n' +
'Accuracy: ' + position.coords.accuracy + '\n' +
'Altitude Accuracy: ' + position.coords.altitudeAccuracy + '\n' +
'Heading: ' + position.coords.heading + '\n' +
'Speed: ' + position.coords.speed + '\n' +
'Timestamp: ' + position.timestamp + '\n');
}
</code></pre>
<hr>
<h1><a name="geolocationError">geolocationError</a></h1>
<p>The user's callback function that is called when there is an error for geolocation functions.</p>
<pre class="prettyprint"><code>function(error) {
// Handle the error
}
</code></pre>
<h2>
<a name="geolocationError_parameters">Parameters</a>
</h2>
<ul>
<li>
<strong>error:</strong> The error returned by the device. (<code><a href="cordova_geolocation_geolocation.md.html#PositionError">PositionError</a></code>)</li>
</ul>
<hr>
<h1><a name="geolocationOptions">geolocationOptions</a></h1>
<p>Optional parameters to customize the retrieval of the geolocation
<code><a href="cordova_geolocation_geolocation.md.html#Position">Position</a></code>.</p>
<pre class="prettyprint"><code>{ maximumAge: 3000, timeout: 5000, enableHighAccuracy: true };
</code></pre>
<h2>
<a name="geolocationOptions_options">Options</a>
</h2>
<ul>
<li>
<strong>enableHighAccuracy:</strong> Provides a hint that the application would like to receive the best possible results. By default, the device will attempt to retrieve a <code><a href="cordova_geolocation_geolocation.md.html#Position">Position</a></code> using network-based methods. Setting this property to <code>true</code> tells the framework to use more accurate methods, such as satellite positioning. <em>(Boolean)</em>
</li>
<li>
<strong>timeout:</strong> The maximum length of time (milliseconds) that is allowed to pass from the call to <code><a href="cordova_geolocation_geolocation.md.html#geolocation.getCurrentPosition">geolocation.getCurrentPosition</a></code> or <code><a href="cordova_geolocation_geolocation.md.html#geolocation.watchPosition">geolocation.watchPosition</a></code> until the corresponding <code><a href="cordova_geolocation_geolocation.md.html#geolocationSuccess">geolocationSuccess</a></code> callback is invoked. If the <code><a href="cordova_geolocation_geolocation.md.html#geolocationSuccess">geolocationSuccess</a></code> callback is not invoked within this time, the <code><a href="cordova_geolocation_geolocation.md.html#geolocationError">geolocationError</a></code> callback will be invoked with a <code><a href="cordova_geolocation_geolocation.md.html#PositionError">PositionError</a>.TIMEOUT</code> error code. NOTE: when used in conjunction with <code><a href="cordova_geolocation_geolocation.md.html#geolocation.watchPosition">geolocation.watchPosition</a></code>, the <code><a href="cordova_geolocation_geolocation.md.html#geolocationError">geolocationError</a></code> callback could be called on an interval every <code>timeout</code> milliseconds! <em>(Number)</em>
</li>
<li>
<strong>maximumAge:</strong> Accept a cached position whose age is no greater than the specified time in milliseconds. <em>(Number)</em>
</li>
</ul>
<h2>
<a name="geolocationOptions_android_quirks">Android Quirks</a>
</h2>
<p>The Android 2.x simulators will not return a geolocation result unless the enableHighAccuracy option is set to true.</p>
<pre class="prettyprint"><code>{ enableHighAccuracy: true }
</code></pre>
</div>
</div>
<!-- Functionality and Syntax Highlighting -->
<script type="text/javascript" src="index.js"></script><script type="text/javascript" src="prettify/prettify.js"></script>
</body>
</html>
Jump to Line
Something went wrong with that request. Please try again.