Gears Future APIs: Location API
I have spoken at a bunch of conferences in Europe this quarter. From the Future of Web Apps, and @mediaAjax in London, to JavaZone and JavaPolis in Oslo and Belgium. When I speak about Gears there, I get a lot of questions about Mobile Gears.
A lot of the features of Gears arguably make even MORE sense on a mobile device. Allowing Web developers to build applications for phones has taken off well thanks to the iPhone. Gears can help out in these high latency devices.
One very handy API to have would be a Location API (although it would be useful in other contexts too):
The purpose of this API is to provide means to fetch the location of a device running a Web browser with Gears.
The Location API is an abstraction for the various LBS APIs that currently exist on mobile platforms (GPS-based, network/cellid-based). The API consists of the Location class, which encapsulates various location attributes (latitude, longitude, etc), and also provides the means to query the platform for a location fix. This API also adds a new event type that is fired every time the location changes. Location implementations can be straightforward mappings to native LBS APIs (e.g the S60 Location Acquisition API) or have a more complex design that combines several location providers (e.g a GPS-based provider and a cell id-based provider) and returns the location from the most accurate provider at any given time.
Here is the API as a code example using it:
// Getting the object var location = google.gears.factory.create( "beta.location", "1.0" ); // Setting up a callback to handle "location changed" events location.onlocationstatechanged = function() { switch (this.state) { case 1: SetStatusText("Connecting"); break; case 2: SetStatusText("Acquiring"); break; case 3: SetStatusText("Location accuracy:", this.accuracy); MoveMap(this.latitude, this.longitude); break; case 5: HandleError(this.error); break; default: alert("Unknown state!"); } } // Initiate a fix. This leads to the onlocationstatechanged event handler being called exactly once for each // of the "connecting" and "acquiring" states and one or more times for the "fixed" state (for the initial // fix and every time the location changes, after that). location.startLocationUpdates(); // async call, initiates fix (powers up GPS if needed, etc) ... // Getting the last known location if (location.latitude != -1 && location.timeUTC > threshold) { // the location info is valid and not very old Foo(location.latitude, location.longitude); } // Cancel the request. This leads to the onlocationstatechanged event handler being called for // the "canceled" state. This call will power down the GPS HW / close HTTP connection // (depending on the location providers that were in use). location.stopLocationUpdates();
I can imagine the fun games that I could write here, let alone the interesting business apps that could take the location context into consideration.
Other Future APIs
Disclaimer: This is early days, and who knows what the final API will look like, or if it will even make it. Do you have ideas for cool Gears that make the Web better? Let us know!.