Revision 100680 of Using geolocation

  • Enlace amigable (slug) de la revisión: Using_geolocation
  • Título de la revisión: Using geolocation
  • Id de la revisión: 100680
  • Creada:
  • Creador: codigoreal
  • ¿Es la revisión actual? No
  • Comentario page created, 982 words added

Contenido de la revisión

Usar la Geolocalización

{{ gecko_minversion_header("1.9.1") }}

Firefox 3.5 y versiones anteriores admiten la API de Geolocalización; esto permite al usuario indicar su ubicación a las aplicaciones web.  .Por seguridad se le pide al usuario que confirme el permiso para indicar información de su ubicación

Firefox 3.5 includes support for locating you based on your WiFi information using Google Location Services. In the transaction between Firefox 3.5 and Google, data is exchanged including WiFi Access Point data, an access token (similar to a 2 week cookie), and the user's IP address.  For more information, please check out Mozilla's Privacy Policy and Google's Privacy Policy covering how this data can be used.  Also see the source code if you're curious how this service is implemented.

Note: If your interest is in writing a geolocation provider -- a service which can be queried for the user's location -- please see {{ Interface("nsIGeolocationProvider") }}.  You may also find the WiFi access point monitoring serviceinteresting.

Firefox 3.6 (Gecko 1.9.2) added support for using the GPSD (GPS daemon) service for geolocation on Linux.

The geolocation object

The geolocation API is published through a geolocation child object within the navigator object.  If the object exists, geolocation services are available.  You can test for the presence of geolocation thusly:

if (navigator.geolocation) {
  /* geolocation is available */
} else {
  alert("I'm sorry, but geolocation services are not supported by your browser.");
}

{{ gecko_minversion_header("1.9.2") }}

Starting in Gecko 1.9.2 (Firefox 3.6), add-ons can obtain the geolocation object by obtaining a reference to the geolocation service like this:

var geolocation = Components.classes["@mozilla.org/geolocation;1"]
                            .getService(Components.interfaces.nsIDOMGeoGeolocation);

Getting the current position

To obtain the user's current location, you can call the getCurrentPosition() method.  This initiates an asynchronous request to detect the user's position, and queries the positioning hardware to get up-to-date information.  When the position is determined, a specified callback routine is executed.  You can optionally provide a second callback to be executed if an error occurs.  A third, optional, parameter is an options interface where you can set the maximum age of the request, and the time to wait for a request.

Use getCurrentPosition() if you want a single position fix ASAP, regardless of the accuracy. Devices with a GPS, for example, can take a minute or more to get a GPS fix, so less accurate data (IP location or wifi) may be returned togetCurrentPosition() to start.

navigator.geolocation.getCurrentPosition(function(position) {
  do_something(position.coords.latitude, position.coords.longitude);
});

The above example will cause the do_something() function to execute when the location is obtained.

Watching the current position

If the position data changes (either by device movement or if more accurate geo information arrives) , you can set up a callback that is called with that updated position information.  This is done using the watchPosition() function, which has the same input parameters as getCurrentPosition().  Its callback is called multiple times, allowing the browser to either update your location as you move, or provide a more accurate location as different techniques are used to geolocate you.  The error callback, which is optional just as it is for getCurrentPosition(), is called only once, if there will never be valid results returned.

You can use watchPosition() without an initial getCurrentPosition() call.

var watchID = navigator.geolocation.watchPosition(function(position) {
  do_something(position.coords.latitude, position.coords.longitude);
}
);

The watchPosition() method returns an ID number that can be used to uniquely identify the requested position watcher; you use this value in tandem with the clearWatch() method to stop watching the user's location.

navigator.geolocation.clearWatch(watchID);

watchPosition() accepts a success callback and error callback (like getCurrentPosition) and an optional positionObjectsobject, which can have three properties:

  • enableHighAccuracy – A boolean which indicates to the device that you wish to obtain it’s most accurate readings (this parameter may or may not make a difference, depending on your hardware)
  • maximumAge – The maximum age (in milliseconds) of the reading (this is appropriate as the device may cache readings to save power and/or bandwidth)
  • timeout – The maximum time (in milliseconds) for which you are prepared to allow the device to try to obtain a Geo location

A call to watchPosition could look like:

var wpid = navigator.geolocation.watchPosition(geo_success, geo_error, {enableHighAccuracy:true, maximumAge:30000, timeout:27000});

A demo of watchPosition in use: http://www.thedotproduct.org/experiments/geo/


Describing a position

The user's location is described using a Position object, which has the following fields:

timestamp
The time at which the reading was taken, as a DOMTimeStamp.
coords
An nsIDOMGeoPositionCoords object indicating the location.
address {{ gecko_minversion_inline("1.9.2") }}
An {{ interface("nsIDOMGeoPositionAddress") }} object specifying the corresponding address, if available.

Handling errors

The error callback, if provided when calling getCurrentPosition() or watchPosition(), has the following signature:

function errorCallback(PositionError error);

The PositionError structure has the following fields:

code
A numeric error code which much be one of the following:
UNKNOWN_ERROR (numeric value 0)
The location acquisition process failed due to an error not covered by the definition of any other error code in this interface.
PERMISSION_DENIED (numeric value 1)
The location acquisition process failed because the application origin does not have permission to use the Geolocation API.
POSITION_UNAVAILABLE (numeric value 2)
The position of the device could not be determined. One or more of the location providers used in the location acquisition process reported an internal error that caused the process to fail entirely.
TIMEOUT (numeric value 3)
The specified maximum length of time has elapsed before the implementation could successfully acquire a new Position object.
message
A human readable error message, suitable for use in logs and debugging -- but not for display to the user.

See also

  • {{ Interface("nsIGeolocationProvider") }}
  • {{ Interface("nsIDOMGeolocation") }}
  • {{ Interface("nsIDOMGeoPosition") }}
  • {{ Interface("nsIDOMGeoPositionCallback") }}
  • {{ Interface("nsIDOMGeoPositionError") }}
  • {{ Interface("nsIDOMGeoPositionErrorCallback") }}
  • {{ Interface("nsIDOMGeoPositionOptions") }}
  • {{ Interface("nsIDOMNavigatorGeolocation") }}
  • Geolocation API on w3.org 

{{ HTML5ArticleTOC() }}

{{ languages( { "ja": "ja/Using_geolocation" } ) }}

Fuente de la revisión

<div style="background-color: rgb(255, 255, 255); color: rgb(51, 51, 51); font: normal normal normal 0.875em/1.286 'Lucida Grande', 'Lucida Sans Unicode', Lucida, Arial, Helvetica, sans-serif; padding-top: 10px; padding-right: 10px; padding-bottom: 10px; padding-left: 10px; ">
<h1 id="Usar_la_Geolocalización">Usar la Geolocalización</h1>
<div style="background-color: rgb(255, 255, 255); color: rgb(51, 51, 51); font: normal normal normal 0.875em/1.286 'Lucida Grande', 'Lucida Sans Unicode', Lucida, Arial, Helvetica, sans-serif; padding-top: 10px; padding-right: 10px; padding-bottom: 10px; padding-left: 10px; ">
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 1.286em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">{{ gecko_minversion_header("1.9.1") }}</p>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 1.286em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">Firefox 3.5 y versiones anteriores admiten la API de Geolocalización; esto permite al usuario indicar su ubicación a las aplicaciones web.  .Por seguridad se le pide al usuario que confirme el permiso para indicar información de su ubicación</p>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 1.286em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">Firefox 3.5 includes support for locating you based on your WiFi information using Google Location Services. In the transaction between Firefox 3.5 and Google, data is exchanged including WiFi Access Point data, an access token (similar to a 2 week cookie), and the user's IP address.  For more information, please check out Mozilla's <a class="external" href="http://www.mozilla.com/en-US/legal/privacy/" style="text-decoration: none; color: rgb(51, 102, 153) !important; cursor: default; " title="http://www.mozilla.com/en-US/legal/privacy/">Privacy Policy</a> and Google's <a class="external" href="http://www.google.com/privacy-lsf.html" style="text-decoration: none; color: rgb(51, 102, 153) !important; cursor: default; " title="http://www.google.com/privacy-lsf.html">Privacy Policy</a> covering how this data can be used.  Also see the <a class="external" href="http://mxr.mozilla.org/mozilla1.9.1/source/dom/src/geolocation/NetworkGeolocationProvider.js" style="text-decoration: none; color: rgb(51, 102, 153) !important; cursor: default; " title="http://mxr.mozilla.org/mozilla1.9.1/source/dom/src/geolocation/NetworkGeolocationProvider.js">source code</a> if you're curious how this service is implemented.</p>
<div class="note" style="margin-bottom: 1.286em; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: rgb(221, 218, 170); border-right-color: rgb(221, 218, 170); border-bottom-color: rgb(221, 218, 170); border-left-color: rgb(221, 218, 170); border-width: initial; padding-top: 0.75em; padding-right: 15px; padding-bottom: 0.75em; padding-left: 15px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgb(250, 249, 226); color: rgb(93, 86, 54); border-width: initial; border-top-width: 1px; border-right-width: 0px; border-bottom-width: 1px; border-left-width: 0px; background-position: initial initial; background-repeat: initial initial; "><strong>Note:</strong> If your interest is in writing a geolocation provider -- a service which can be queried for the user's location -- please see {{ Interface("nsIGeolocationProvider") }}.  You may also find the <a class="internal" href="/En/Monitoring_WiFi_access_points" style="text-decoration: none; color: rgb(51, 102, 153) !important; cursor: default; " title="En/Monitoring WiFi access points">WiFi access point monitoring service</a>interesting.</div>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 1.286em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">Firefox 3.6 (Gecko 1.9.2) added support for using the <a class=" external" href="http://gpsd.berlios.de/" style="text-decoration: none; color: rgb(51, 102, 153) !important; cursor: default; " title="http://gpsd.berlios.de/">GPSD</a> (GPS daemon) service for geolocation on Linux.</p>
<h2 id="The_geolocation_object">The geolocation object</h2>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 1.286em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">The geolocation API is published through a <code style="font: normal normal normal 100%/normal 'Courier New', 'Andale Mono', monospace; color: inherit; font-weight: inherit; ">geolocation</code> child object within the <code style="font: normal normal normal 100%/normal 'Courier New', 'Andale Mono', monospace; color: inherit; font-weight: inherit; ">navigator</code> object.  If the object exists, geolocation services are available.  You can test for the presence of geolocation thusly:</p>
<pre class="brush: js" style="margin-top: 0px; margin-right: 0px; margin-bottom: 1.286em; margin-left: 0px; padding-top: 10px; padding-right: 20px; padding-bottom: 10px; padding-left: 20px; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: rgb(213, 205, 187); border-right-color: rgb(213, 205, 187); border-bottom-color: rgb(213, 205, 187); border-left-color: rgb(213, 205, 187); overflow-x: auto; overflow-y: auto; font: normal normal normal 100%/normal 'Courier New', 'Andale Mono', monospace; ">if (navigator.geolocation) {
  /* geolocation is available */
} else {
  alert("I'm sorry, but geolocation services are not supported by your browser.");
}
</pre>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 1.286em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">{{ gecko_minversion_header("1.9.2") }}</p>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 1.286em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">Starting in Gecko 1.9.2 (Firefox 3.6), add-ons can obtain the geolocation object by obtaining a reference to the geolocation service like this:</p>
<pre class="brush: js" style="margin-top: 0px; margin-right: 0px; margin-bottom: 1.286em; margin-left: 0px; padding-top: 10px; padding-right: 20px; padding-bottom: 10px; padding-left: 20px; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: rgb(213, 205, 187); border-right-color: rgb(213, 205, 187); border-bottom-color: rgb(213, 205, 187); border-left-color: rgb(213, 205, 187); overflow-x: auto; overflow-y: auto; font: normal normal normal 100%/normal 'Courier New', 'Andale Mono', monospace; ">var geolocation = Components.classes["@mozilla.org/geolocation;1"]
                            .getService(Components.interfaces.nsIDOMGeoGeolocation);
</pre>
<h3 id="Getting_the_current_position">Getting the current position</h3>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 1.286em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">To obtain the user's current location, you can call the <code style="font: normal normal normal 100%/normal 'Courier New', 'Andale Mono', monospace; color: inherit; font-weight: inherit; ">getCurrentPosition()</code> method.  This initiates an asynchronous request to detect the user's position, and queries the positioning hardware to get up-to-date information.  When the position is determined, a specified callback routine is executed.  You can optionally provide a second callback to be executed if an error occurs.  A third, optional, parameter is an options interface where you can set the maximum age of the request, and the time to wait for a request.</p>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 1.286em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">Use <code style="font: normal normal normal 100%/normal 'Courier New', 'Andale Mono', monospace; color: inherit; font-weight: inherit; ">getCurrentPosition()</code> if you want a single position fix ASAP, regardless of the accuracy. Devices with a GPS, for example, can take a minute or more to get a GPS fix, so less accurate data (IP location or wifi) may be returned to<code style="font: normal normal normal 100%/normal 'Courier New', 'Andale Mono', monospace; color: inherit; font-weight: inherit; ">getCurrentPosition()</code> to start.</p>
<pre class="brush: js" style="margin-top: 0px; margin-right: 0px; margin-bottom: 1.286em; margin-left: 0px; padding-top: 10px; padding-right: 20px; padding-bottom: 10px; padding-left: 20px; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: rgb(213, 205, 187); border-right-color: rgb(213, 205, 187); border-bottom-color: rgb(213, 205, 187); border-left-color: rgb(213, 205, 187); overflow-x: auto; overflow-y: auto; font: normal normal normal 100%/normal 'Courier New', 'Andale Mono', monospace; ">navigator.geolocation.getCurrentPosition(function(position) {
  do_something(position.coords.latitude, position.coords.longitude);
});</pre>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 1.286em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">The above example will cause the <code style="font: normal normal normal 100%/normal 'Courier New', 'Andale Mono', monospace; color: inherit; font-weight: inherit; ">do_something()</code> function to execute when the location is obtained.</p>
<h3 id="Watching_the_current_position">Watching the current position</h3>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 1.286em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">If the position data changes (either by device movement or if more accurate geo information arrives) , you can set up a callback that is called with that updated position information.  This is done using the <code style="font: normal normal normal 100%/normal 'Courier New', 'Andale Mono', monospace; color: inherit; font-weight: inherit; ">watchPosition()</code> function, which has the same input parameters as <code style="font: normal normal normal 100%/normal 'Courier New', 'Andale Mono', monospace; color: inherit; font-weight: inherit; ">getCurrentPosition()</code>.  Its callback is called multiple times, allowing the browser to either update your location as you move, or provide a more accurate location as different techniques are used to geolocate you.  The error callback, which is optional just as it is for <code style="font: normal normal normal 100%/normal 'Courier New', 'Andale Mono', monospace; color: inherit; font-weight: inherit; ">getCurrentPosition()</code>, is called only once, if there will never be valid results returned.</p>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 1.286em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">You can use <code style="font: normal normal normal 100%/normal 'Courier New', 'Andale Mono', monospace; color: inherit; font-weight: inherit; ">watchPosition()</code> without an initial <code style="font: normal normal normal 100%/normal 'Courier New', 'Andale Mono', monospace; color: inherit; font-weight: inherit; ">getCurrentPosition() </code>call.</p>
<pre class="brush: js" style="margin-top: 0px; margin-right: 0px; margin-bottom: 1.286em; margin-left: 0px; padding-top: 10px; padding-right: 20px; padding-bottom: 10px; padding-left: 20px; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: rgb(213, 205, 187); border-right-color: rgb(213, 205, 187); border-bottom-color: rgb(213, 205, 187); border-left-color: rgb(213, 205, 187); overflow-x: auto; overflow-y: auto; font: normal normal normal 100%/normal 'Courier New', 'Andale Mono', monospace; ">var watchID = navigator.geolocation.watchPosition(function(position) {
  do_something(position.coords.latitude, position.coords.longitude);
}
);</pre>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 1.286em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">The <code style="font: normal normal normal 100%/normal 'Courier New', 'Andale Mono', monospace; color: inherit; font-weight: inherit; ">watchPosition()</code> method returns an ID number that can be used to uniquely identify the requested position watcher; you use this value in tandem with the <code style="font: normal normal normal 100%/normal 'Courier New', 'Andale Mono', monospace; color: inherit; font-weight: inherit; ">clearWatch()</code> method to stop watching the user's location.</p>
<pre style="margin-top: 0px; margin-right: 0px; margin-bottom: 1.286em; margin-left: 0px; padding-top: 10px; padding-right: 20px; padding-bottom: 10px; padding-left: 20px; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: rgb(213, 205, 187); border-right-color: rgb(213, 205, 187); border-bottom-color: rgb(213, 205, 187); border-left-color: rgb(213, 205, 187); overflow-x: auto; overflow-y: auto; font: normal normal normal 100%/normal 'Courier New', 'Andale Mono', monospace; ">navigator.geolocation.clearWatch(watchID);
</pre>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 1.286em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><code style="font: normal normal normal 100%/normal 'Courier New', 'Andale Mono', monospace; color: inherit; font-weight: inherit; ">watchPosition() </code>accepts a success callback and error callback (like <code style="font: normal normal normal 100%/normal 'Courier New', 'Andale Mono', monospace; color: inherit; font-weight: inherit; ">getCurrentPosition</code>) and an optional <code style="font: normal normal normal 100%/normal 'Courier New', 'Andale Mono', monospace; color: inherit; font-weight: inherit; ">positionObjects</code>object, which can have three properties:</p>
<ul style="margin-top: 0px; margin-right: 0px; margin-bottom: 1.286em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; list-style-type: none; list-style-position: initial; list-style-image: initial; "> <li><code style="font: normal normal normal 100%/normal 'Courier New', 'Andale Mono', monospace; color: inherit; font-weight: inherit; ">enableHighAccuracy</code> – A boolean which indicates to the device that you wish to obtain it’s most accurate readings (this parameter may or may not make a difference, depending on your hardware)</li> <li><code style="font: normal normal normal 100%/normal 'Courier New', 'Andale Mono', monospace; color: inherit; font-weight: inherit; ">maximumAge</code> – The maximum age (in milliseconds) of the reading (this is appropriate as the device may cache readings to save power and/or bandwidth)</li> <li><code style="font: normal normal normal 100%/normal 'Courier New', 'Andale Mono', monospace; color: inherit; font-weight: inherit; ">timeout</code> – The maximum time (in milliseconds) for which you are prepared to allow the device to try to obtain a Geo location</li>
</ul>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 1.286em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">A call to watchPosition could look like:</p>
<pre style="margin-top: 0px; margin-right: 0px; margin-bottom: 1.286em; margin-left: 0px; padding-top: 10px; padding-right: 20px; padding-bottom: 10px; padding-left: 20px; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: rgb(213, 205, 187); border-right-color: rgb(213, 205, 187); border-bottom-color: rgb(213, 205, 187); border-left-color: rgb(213, 205, 187); overflow-x: auto; overflow-y: auto; font: normal normal normal 100%/normal 'Courier New', 'Andale Mono', monospace; ">var wpid = navigator.geolocation.watchPosition(geo_success, geo_error, {enableHighAccuracy:true, maximumAge:30000, timeout:27000});</pre>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 1.286em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><a id="fck_paste_padding" style="text-decoration: none; color: rgb(51, 102, 153) !important; cursor: default; ">A demo of watchPosition in use: </a><a class=" external" href="http://www.thedotproduct.org/experiments/geo/" style="text-decoration: none; color: rgb(51, 102, 153) !important; cursor: default; ">http://www.thedotproduct.org/experiments/geo/</a><br>
<a id="fck_paste_padding" style="text-decoration: none; color: rgb(51, 102, 153) !important; cursor: default; "></a></p>
<h2 id="Describing_a_position">Describing a position</h2>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 1.286em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">The user's location is described using a Position object, which has the following fields:</p>
<dl style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "> <dt style="font-family: Georgia, Times, 'Times New Roman', serif; font-style: normal; font-weight: bold; ">timestamp</dt> <dd style="margin-bottom: 20px; padding-left: 16px; border-bottom-width: 1px; border-bottom-style: dashed; border-bottom-color: rgb(153, 153, 153); margin-top: 0px; margin-right: 0px; margin-left: 0px; padding-top: 2px; padding-right: 0px; padding-bottom: 4px; ">The time at which the reading was taken, as a <code style="font: normal normal normal 100%/normal 'Courier New', 'Andale Mono', monospace; color: inherit; font-weight: inherit; ">DOMTimeStamp</code>.</dd> <dt style="font-family: Georgia, Times, 'Times New Roman', serif; font-style: normal; font-weight: bold; ">coords</dt> <dd style="margin-bottom: 20px; padding-left: 16px; border-bottom-width: 1px; border-bottom-style: dashed; border-bottom-color: rgb(153, 153, 153); margin-top: 0px; margin-right: 0px; margin-left: 0px; padding-top: 2px; padding-right: 0px; padding-bottom: 4px; ">An <a class="internal" href="/en/XPCOM_Interface_Reference/NsIDOMGeoPositionCoords" style="text-decoration: none; color: rgb(51, 102, 153) !important; cursor: default; " title="En/NsIDOMGeoPositionCoords"><code style="font: normal normal normal 100%/normal 'Courier New', 'Andale Mono', monospace; color: inherit; font-weight: inherit; ">nsIDOMGeoPositionCoords</code></a> object indicating the location.</dd> <dt style="font-family: Georgia, Times, 'Times New Roman', serif; font-style: normal; font-weight: bold; ">address {{ gecko_minversion_inline("1.9.2") }}</dt> <dd style="margin-bottom: 20px; padding-left: 16px; border-bottom-width: 1px; border-bottom-style: dashed; border-bottom-color: rgb(153, 153, 153); margin-top: 0px; margin-right: 0px; margin-left: 0px; padding-top: 2px; padding-right: 0px; padding-bottom: 4px; ">An {{ interface("nsIDOMGeoPositionAddress") }} object specifying the corresponding address, if available.</dd>
</dl>
<h2 id="Handling_errors">Handling errors</h2>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 1.286em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">The error callback, if provided when calling <code style="font: normal normal normal 100%/normal 'Courier New', 'Andale Mono', monospace; color: inherit; font-weight: inherit; ">getCurrentPosition()</code> or <code style="font: normal normal normal 100%/normal 'Courier New', 'Andale Mono', monospace; color: inherit; font-weight: inherit; ">watchPosition()</code>, has the following signature:</p>
<pre style="margin-top: 0px; margin-right: 0px; margin-bottom: 1.286em; margin-left: 0px; padding-top: 10px; padding-right: 20px; padding-bottom: 10px; padding-left: 20px; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: rgb(213, 205, 187); border-right-color: rgb(213, 205, 187); border-bottom-color: rgb(213, 205, 187); border-left-color: rgb(213, 205, 187); overflow-x: auto; overflow-y: auto; font: normal normal normal 100%/normal 'Courier New', 'Andale Mono', monospace; ">function errorCallback(PositionError error);
</pre>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 1.286em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">The <code style="font: normal normal normal 100%/normal 'Courier New', 'Andale Mono', monospace; color: inherit; font-weight: inherit; ">PositionError</code> structure has the following fields:</p>
<dl style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "> <dt style="font-family: Georgia, Times, 'Times New Roman', serif; font-style: normal; font-weight: bold; ">code</dt> <dd style="margin-bottom: 20px; padding-left: 16px; border-bottom-width: 1px; border-bottom-style: dashed; border-bottom-color: rgb(153, 153, 153); margin-top: 0px; margin-right: 0px; margin-left: 0px; padding-top: 2px; padding-right: 0px; padding-bottom: 4px; ">A numeric error code which much be one of the following:</dd> <dt style="font-family: Georgia, Times, 'Times New Roman', serif; font-style: normal; font-weight: bold; "><dfn id="unknown_error"><code style="font: normal normal normal 100%/normal 'Courier New', 'Andale Mono', monospace; color: inherit; font-weight: inherit; ">UNKNOWN_ERROR</code></dfn> (numeric value 0)</dt> <dd style="margin-bottom: 20px; padding-left: 16px; border-bottom-width: 1px; border-bottom-style: dashed; border-bottom-color: rgb(153, 153, 153); margin-top: 0px; margin-right: 0px; margin-left: 0px; padding-top: 2px; padding-right: 0px; padding-bottom: 4px; ">The location acquisition process failed due to an error not covered by the definition of any other error code in this interface.</dd> <dt style="font-family: Georgia, Times, 'Times New Roman', serif; font-style: normal; font-weight: bold; "><dfn id="permission_denied_error"><code style="font: normal normal normal 100%/normal 'Courier New', 'Andale Mono', monospace; color: inherit; font-weight: inherit; ">PERMISSION_DENIED</code></dfn> (numeric value 1)</dt> <dd style="margin-bottom: 20px; padding-left: 16px; border-bottom-width: 1px; border-bottom-style: dashed; border-bottom-color: rgb(153, 153, 153); margin-top: 0px; margin-right: 0px; margin-left: 0px; padding-top: 2px; padding-right: 0px; padding-bottom: 4px; ">The location acquisition process failed because the application origin does not have permission to use the Geolocation API.</dd> <dt style="font-family: Georgia, Times, 'Times New Roman', serif; font-style: normal; font-weight: bold; "><dfn id="position_unavailable_error"><code style="font: normal normal normal 100%/normal 'Courier New', 'Andale Mono', monospace; color: inherit; font-weight: inherit; ">POSITION_UNAVAILABLE</code></dfn> (numeric value 2)</dt> <dd style="margin-bottom: 20px; padding-left: 16px; border-bottom-width: 1px; border-bottom-style: dashed; border-bottom-color: rgb(153, 153, 153); margin-top: 0px; margin-right: 0px; margin-left: 0px; padding-top: 2px; padding-right: 0px; padding-bottom: 4px; ">The position of the device could not be determined. One or more of the location providers used in the location acquisition process reported an internal error that caused the process to fail entirely.</dd> <dt style="font-family: Georgia, Times, 'Times New Roman', serif; font-style: normal; font-weight: bold; "><dfn id="timeout_error"><code style="font: normal normal normal 100%/normal 'Courier New', 'Andale Mono', monospace; color: inherit; font-weight: inherit; ">TIMEOUT</code></dfn> (numeric value 3)</dt> <dd style="margin-bottom: 20px; padding-left: 16px; border-bottom-width: 1px; border-bottom-style: dashed; border-bottom-color: rgb(153, 153, 153); margin-top: 0px; margin-right: 0px; margin-left: 0px; padding-top: 2px; padding-right: 0px; padding-bottom: 4px; ">The specified maximum length of time has elapsed before the implementation could successfully acquire a new Position object.</dd> <dd style="margin-bottom: 20px; padding-left: 16px; border-bottom-width: 1px; border-bottom-style: dashed; border-bottom-color: rgb(153, 153, 153); margin-top: 0px; margin-right: 0px; margin-left: 0px; padding-top: 2px; padding-right: 0px; padding-bottom: 4px; "> </dd><dt style="font-family: Georgia, Times, 'Times New Roman', serif; font-style: normal; font-weight: bold; ">message</dt> <dd style="margin-bottom: 20px; padding-left: 16px; border-bottom-width: 1px; border-bottom-style: dashed; border-bottom-color: rgb(153, 153, 153); margin-top: 0px; margin-right: 0px; margin-left: 0px; padding-top: 2px; padding-right: 0px; padding-bottom: 4px; ">A human readable error message, suitable for use in logs and debugging -- but not for display to the user.</dd>
</dl>
<h2 id="See_also">See also</h2>
<ul style="margin-top: 0px; margin-right: 0px; margin-bottom: 1.286em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; list-style-type: none; list-style-position: initial; list-style-image: initial; "> <li>{{ Interface("nsIGeolocationProvider") }}</li> <li>{{ Interface("nsIDOMGeolocation") }}</li> <li>{{ Interface("nsIDOMGeoPosition") }}</li> <li>{{ Interface("nsIDOMGeoPositionCallback") }}</li> <li>{{ Interface("nsIDOMGeoPositionError") }}</li> <li>{{ Interface("nsIDOMGeoPositionErrorCallback") }}</li> <li>{{ Interface("nsIDOMGeoPositionOptions") }}</li> <li>{{ Interface("nsIDOMNavigatorGeolocation") }}</li> <li><a class="external" href="http://dev.w3.org/geo/api/spec-source.html" style="text-decoration: none; color: rgb(51, 102, 153) !important; cursor: default; " title="http://dev.w3.org/geo/api/spec-source.html">Geolocation API on w3.org</a> </li>
</ul>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 1.286em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">{{ HTML5ArticleTOC() }}</p>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 1.286em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">{{ languages( { "ja": "ja/Using_geolocation" } ) }}</p>
</div>
</div>

Revertir a esta revisión