Info and examples from W3C Geolocation API Specification:
The Geolocation API defines a high-level interface to location
information associated only with the device hosting the
implementation, such as latitude and longitude. The API itself is
agnostic of the underlying location information sources. 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, as well
as user input. No guarantee is given that the API returns the device's
actual location.
You could implement it this way:
(one-shot position request)
function showMap(position) {
// Show a map centered at (position.coords.latitude, position.coords.longitude).
}
// One-shot position request.
navigator.geolocation.getCurrentPosition(showMap);
(requesting repeated position updates)
function scrollMap(position) {
// Scrolls the map so that it is centered at (position.coords.latitude, position.coords.longitude).
}
// Request repeated updates.
var watchId = navigator.geolocation.watchPosition(scrollMap);
function buttonClickHandler() {
// Cancel the updates when the user clicks a button.
navigator.geolocation.clearWatch(watchId);
}