|
本帖最后由 demo 于 2014-3-4 04:04 编辑
The geolocation API allows the user to provide their location to web applications if they so desire. For privacy reasons, the user is asked for permission to report location information.
The geolocation objectThe geolocation API is published through the navigator.geolocation object.
If the object exists, geolocation services are available. You can test for the presence of geolocation thusly:
- if ("geolocation" in navigator) {
- /* geolocation is available */
- } else {
- /* geolocation IS NOT available */
- }
复制代码
Note: On Firefox 24 and older versions, "geolocation" in navigator always returned true even if the API was disabled. This has been fixed with Firefox 25 to comply with the spec. (bug 884921).
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, the defined callback function is executed. You can optionally provide a second callback function to be executed if an error occurs. A third, optional, parameter is an options object where you can set the maximum age of the position returned, the time to wait for a request, and if you want high accuracy for the position.
Note: By default, getCurrentPosition() tries to answer as fast as possible with a low accuracy result. It is useful if you need a quick answer 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 getCurrentPosition().
- 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 function that is called with that updated position information. This is done using the watchPosition() function, which has the same input parameters as getCurrentPosition(). The callback function 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 function, which is optional just as it is for getCurrentPosition(), can be called repeatedly.
Note: 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);
复制代码
Fine tuning response
Both getCurrentPosition() and watchPosition() accept a success callback, an optional error callback, and an optional PositionOptions object.
A call to watchPosition could look like:
- function geo_success(position) {
- do_something(position.coords.latitude, position.coords.longitude);
- }
- function geo_error() {
- alert("Sorry, no position available.");
- }
- var geo_options = {
- enableHighAccuracy: true,
- maximumAge : 30000,
- timeout : 27000
- };
- var wpid = navigator.geolocation.watchPosition(geo_success, geo_error, geo_options);
复制代码 [url=]A demo of watchPosition in use:[/url]http://www.thedotproduct.org/experiments/geo/
|
|