I created a small jQuery plugin which acts as a simplification of the Geolocation API.
Instead of using navigator.geolocation.getCurrentPosition you can now just use the jQuery methods $.geolocation.get()
or $.geolocation.watch()
.
Contrary to the standard API the only parameter the functions expect is a JSON object with three properties in no particular order: success, error, options. For success and error you can also use their alias properties „win“ and „fail“:
$.geolocation.get({win: function() {}, fail: function() {}, options);
You can also use $.geolocation.getCurrentPosition(success, error, options)
to get native API feeling if this makes you happier. In conjunction with my Geolocation API polyfill script this also works with some non-standard Geolocation APIs like Google Gears or Blackberry Location.
Usage
- $.geolocation.clearWatch(watchID)
- Stops tracking of the user for the according watchID.
- watchID (Integer)
- $.geolocation.get(options)
- Get the current position of the user
- options (Object)
- error
Function to call if geolocation request failed
- fail
Alias for error
- options
Options for the geolocation request
- enableHighAccuracy
- maximumAge
- timeout
- success
Function to call if geolocation request was successful
- win
Alias for success
- $.geolocation.getCurrentPosition(success, error, options)
- Get the current position of the user (API standard behavior)
- success Function to call if geolocation request was successful
- error Function to call if geolocation request failed
- options Options for the geolocation request
- enableHighAccuracy
- maximumAge
- timeout
- $.geolocation.stop(watchID)
- Stops tracking of the user for the according watchID.
- watchID (Integer)
- $.geolocation.stopAll()
- Stops all watchPosition callbacks.
- $.geolocation.watch(options)
- Track the movement of the user
- Returns: watchID (Integer)
- options (Object)
- error
Function to call if geolocation request failed
- fail
Alias for error
- options
Options for the geolocation request
- enableHighAccuracy
- maximumAge
- timeout
- success
Function to call if geolocation request was successful
- win
Alias for success
- $.geolocation.watchPosition(success, error, options)
- Track the movement of the user (API standard behavior)
- Returns: watchID (Integer)
- success Function to call if geolocation request was successful
- error Function to call if geolocation request failed
- options Options for the geolocation request
- enableHighAccuracy
- maximumAge
- timeout
Examples
function alertMyPosition(position) {
alert("Your position is " + position.coords.latitude + ", " + position.coords.longitude);
}
function noLocation(error) {
alert("No location info available. Error code: " + error.code);
}
$('#getPositionButton').bind('click', function() {
$.geolocation.get({win: alertMyPosition, fail: noLocation});
});
$('#watchPositionButton').bind('click', function() {
// alertMyPosition is called each time the user's position changes
myPosition = $.geolocation.watch({win: alertMyPosition});
});
$('#stopButton').bind('click', function() {
$.geolocation.stop(myPosition);
}); |
function alertMyPosition(position) {
alert("Your position is " + position.coords.latitude + ", " + position.coords.longitude);
}
function noLocation(error) {
alert("No location info available. Error code: " + error.code);
}
$('#getPositionButton').bind('click', function() {
$.geolocation.get({win: alertMyPosition, fail: noLocation});
});
$('#watchPositionButton').bind('click', function() {
// alertMyPosition is called each time the user's position changes
myPosition = $.geolocation.watch({win: alertMyPosition});
});
$('#stopButton').bind('click', function() {
$.geolocation.stop(myPosition);
});
Demo
http://manuel-bieh.de/publikationen/scripts/jquery/geolocation/
Download
https://github.com/manuelbieh/jQuery-Geolocation