This API is experimental. It is only available to Chrome users on the dev channel.

chrome.location

Description: Use the chrome.location API to retrieve the geographic location of the host machine. This API is a version of the HTML Geolocation API that is compatible with event pages.
Availability: Dev channel only.
Permissions: "location"

Overview

To start retrieving location information, you need to create a location watch by calling location.watchLocation, passing in the location watch details via the requestInfo parameter:

chrome.location.watchLocation(name, requestInfo);

You also need to add a listener for the location.onLocationUpdate event. Chrome will fire this event after you create your location watch, and it will keep firing the event every time the geographic location of the host machine changes, until you call location.clearWatch.

Here's sample code to listen for location updates:

        chrome.location.onLocationUpdate.addListener(function(position) {
          console.log(JSON.stringify(position));
        });
      

Summary

Methods
watchLocation chrome.location.watchLocation(string name, object requestInfo)
clearWatch chrome.location.clearWatch(string name)
Events
onLocationUpdate
onLocationError

Methods

watchLocation

chrome.location.watchLocation(string name, object requestInfo)

Starts a location watching request. If there is another location watching request with the same name (or no name if none is specified), it will be cancelled and replaced by this request.

Parameters
string name Optional name to identify this request. Defaults to the empty string.
object requestInfo Optional parameters for this request.
double (optional) minDistanceInMeters Minimum distance between location updates, in meters. Defaults to 0 - any change in location will be reported.
double (optional) minTimeInMilliseconds Minimum time interval between location updates, in milliseconds. Defaults to 0 - system-defined frequency of updates will be used.
integer (optional) maximumAge Maximum age of a cached location, in milliseconds, that Chrome can pass to the first onLocationUpdate event for this location watch request. If this value <= 0, Chrome will always attempt to acquire a new location. Defaults to 0.

clearWatch

chrome.location.clearWatch(string name)

Ends a location watching request.

Parameters
string name Optional name to identify the request to remove. Defaults to the empty string.

Events

onLocationUpdate

Fired when a location change is detected.

addListener

chrome.location.onLocationUpdate.addListener(function callback)
Parameters
function callback

The callback parameter should be a function that looks like this:

function(object location) {...};
object location An object containing matching events and new location.
string name Location watch request name.
object coords Coordinates and their accuracy.
double latitude The geographic latitude specified in degrees.
double longitude The geographic longitude specified in degrees.
double (optional) altitude The height of the position, specified in meters above the [WGS84] ellipsoid, or null if Chrome cannot provide altitude information.
double accuracy The accuracy of the latitude and longitude coordinates, in meters. This represents the radius of a circle around the given location.
double (optional) altitudeAccuracy The accuracy in meters of the 'altitude' attribute if it's not null, otherwise, null.
double (optional) heading The direction of travel of the hosting device and is specified in degrees, where 0 <= heading < 360, counting clockwise relative to the true north. If the Chrome cannot provide heading information, the value of this attribute is null. If the hosting device is stationary (i.e. the value of the speed attribute is 0), then the value of the heading attribute is NaN.
double (optional) speed The magnitude of the horizontal component of the hosting device's current velocity and is specified in meters per second. If the Chrome cannot provide speed information, the value of this attribute is null.
double timestamp The time when the Location object was acquired in milliseconds since the start of the Unix Epoch.

onLocationError

Fired when detecting location in not possible.

addListener

chrome.location.onLocationError.addListener(function callback)
Parameters
function callback

The callback parameter should be a function that looks like this:

function(string error) {...};
string error Human-readable error description.