| FireFox | Safari | Chrome | Opera | iPhone | Android |
|---|---|---|---|---|---|
| 3.5+ | 5.0+ | 5.0+ | 10.6+ | 3.0+ | 2.0+ |
if (navigator.geolocation) {
// geolocation is available. yay!
// do stuff.
} else {
// bugger
alert ('Geolocation services are not supported by your browser');
}
// Three parameters - Success callback, an Optional Error callback, and an Optional PositionOptions Object.
navigator.geolocation.getCurrentPosition(show_map, handle_error);
// callback is called asynchronously
// with a Position object
function show_map(position) {
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
var accuracy = position.coords.accuracy;
// what are you waiting for?
}
// things gone awry. error callback will be called with PositionError object.
function handle_error(err) {
if (err.code == 1) {
// user said no!
}
}
| Property | Type | Notes |
|---|---|---|
| coords.latitude | double | decimal degrees |
| coords.longitude | double | decimal degrees |
| coords.accuracy | double | meters |
| coords.altitude | double or null | meters |
| coords.altitudeAccuracy | double or null | meters |
| coords.heading | double or null | degrees |
| coords.speed | double or null | meters/second |
| timestamp | DOMTimeStamp | Date() object |
| Error | Enumerated Value |
|---|---|
| PERMISSION_DENIED | 1 |
| POSITION_UNAVAILABLE | 2 |
| TIMEOUT | 3 |
| UNKNOWN_ERROR | 0 |
| Property | Type | Default | Note |
|---|---|---|---|
| enableHighAccuracy | Boolean | false | true might be slower, may fail. |
| timeout | long | (no default) | milliseconds |
| maximumAge | long | 0 | milliseconds |
// Cache a position for 75 seconds
navigator.geolocation.getCurrentPosition(show_map, handle_error, {maximumAge:75000});
// Force to use GPS if available
navigator.geolocation.getCurrentPosition(show_map, handle_error, {enableHighAccuracy: true});
// Fall back to a previously cached position
navigator.geolocation.getCurrentPosition(show_map, handle_error, {maximumAge:Infinity, timeout:0, enableHighAccuracy: true});
// Simple, eh?
var watchId = navigator.geolocation.watchPosition(update_map, handle_error);
// On clicking some button, we want all this to stop.
function someButtonClickHandler() {
navigator.geolocation.clearWatch(watchId);
}
// To set a key-value
setItem(key, value);
// even better
localStorage[key] = value;
// To get a value
string* getItem(key);
// moar better
var someInt = parseInt(localStorage[key]);
// Remove a value
removeItem(key);
// Get a key
string key(index);
// Clears the entire storage
clear();
// Count
.length;
| IE | FireFox | Safari | Chrome | Opera | iPhone | Android |
|---|---|---|---|---|---|---|
| 8.0+ | 3.5+ | 4.0+ | 4.0+ | 10.5+ | 2.0+ | 2.0+ |
if (Modernizr.localstorage) {
// window.localStorage is available!
} else {
// no native support for HTML5 storage :(
// maybe try dojox.storage or a third-party solution
}
// Sample code
openDatabase('documents', '1.0', 'Local document storage', 5*1024*1024, function (db) {
db.changeVersion('', '1.0', function (t) {
t.executeSql('CREATE TABLE docids (id, name)');
}, error);
});
<!DOCTYPE HTML>
<html manifest="cache.manifest">
<body>
...
</body>
</html>
CACHE MANIFEST
#version 3
#URLs to be cached
CACHE:
index.html
css/style.css
js/script.js
# the whitelist
NETWORK:
/live/*
/ajax/*
# fallback(s)
FALLBACK:
/ offline.html
# or foo/bar foo.html