The JSON/REST JavaScript API is designed to provide you with a Google Gears-compatible network location provider. When using the Gears Geolocation object, you can specify the address of the CLP as a network location provider in the JavaScript of a Gears-enabled Web page. For information on the Geolocation API, go to http://code.google.com/apis/gears/api_geolocation.html.
However, if you are using a custom application, the CLP implements the Google Gears Geolocation API network protocol in order to act as a compatible network location provider. The Geolocation API network protocol is a simple JSON over HTTP request/response system with the JSON request payload sent using a HTTP POST operation. In order for the request to be processed and posted, you must specify the attributes for the HTTP request. For information on the Google Gears Geolocation API Network Protocol, go to http://code.google.com/apis/gears/geolocation_network_protocol.html.
This reference describes the JSON/REST JavaScript API, as well as the Geolocation API Network Protocol, used to access the CLP. This reference also includes a sample code that shows how an application attempts to obtain an accurate position from the test CLP by comparing JavaScript code samples from both Clearwire and Google Gears. Also included is a sample code that shows how an application attempts to obtain a position from the test CLP first, and then reverts to the Google location service when unable to retrieve a position from the CLP.
To see a live demo showing the use of both Clearwire and Google location services to retrieve current location, go to http://developer.clear.com/ClearLocationDemo.html. You must be connected to the Clearwire WiMAX network and use a Gears-enabled Web browser to enable the demo.
The JSON/REST JavaScript API is designed to provide you with a Google Gears-compatible network location provider. When using the Gears Geolocation object, you can specify the address of the CLP as a network location provider in the JavaScript of a Gears-enabled Web page.
The following Google Gears PositionOptions class attributes are used to specify the input message for the CLP. For information on the Geolocation API, go to http://code.google.com/apis/gears/api_geolocation.html.
| Attribute | Description | Required | Type |
|---|---|---|---|
| gearsLocationProviderUrls | Specifies the URLs of the providers where the HTTP request is posted to convert geolocation signals into positions. | Yes | string |
| gearsRequestAddress | Requests reverse geocoded address information as part of the position data. | No | boolean |
| gearsLocationProviderUrls | |||
Specifies the URLs of the providers where the HTTP request is posted to convert geolocation signals into positions. The gearsLocationProviderUrls option specifies the URL for the CLP, as well as the URL of the Google location platform, in case the CLP is unable to provide a position and a backup location service is needed. |
|||
| Required | Yes | ||
| Type | string | ||
| Input Value | For this release, the input value is the URL of the test CLP. Example: "http://testlocation.clearwire-wmx.net:8000/json/" | ||
| Return Value | Returns location information, or position data. | ||
| Note: If a device is not connected to WiMAX and the CLP is unable to provide a position, the Google location service is queried to provide a WiFi or an IP location-based position. You could also query the Google location service if you want to go outside of the WiMAX network. | |||
| gearsRequestAddress | |||
| Requests reverse geocoded address information as part of the position data. | |||
| Required | No | ||
| Type | boolean | ||
| Input Value | true | ||
| Return Value | Returns the address object for the location, if available. If unspecified or set to false, no value is returned. |
||
try { var geolocation = google.gears.factory.create('beta.geolocation'); geolocation.getCurrentPosition( successCallback, errorCallback, { enableHighAccuracy: true, gearsRequestAddress: true,gearsLocationProviderUrls: ["http://testlocation.clearwire-wmx.net:8000/json/","http://www.google.com/loc/json"]} ); } catch (e) { setError('Error using Geolocation API: ' + e.message); return; }
If you are using a custom application to communicate with the CLP, the CLP implements the Google Gears Geolocation API network protocol in order to act as a compatible network location provider. The Geolocation API network protocol is a simple JSON over HTTP request/response system with the JSON request payload sent using a HTTP POST operation. If a device is connected to WiMAX, the request is posted to a power system to retrieve location data; if not, the request is posted to Google.
In order for the request to be processed and posted, you must specify the attributes for the HTTP request, as described below. For more information on the Google Gears Geolocation API Network Protocol, go to http://code.google.com/apis/gears/geolocation_network_protocol.html.
The details of the request protocol are as follows.
| host | |||
| The host of the Web page that is requesting the location. | |||
| Required | Yes | ||
| Type | string | ||
| Input Value | Address of the Web site. | ||
| Return Value | None | ||
| radio_type | |||
| Mobile radio type (gsm, cdma, wcdma). The CLP does not validate and run a check on this input value. | |||
| Required | No | ||
| Type | string | ||
| Input Value | For more information, go to http://code.google.com/apis/gears/geolocation_network_protocol.html. | ||
| Return Value | None. | ||
| request_address | |||
| Requests the server to provide an address. | |||
| Required | No | ||
| Type | boolean | ||
| Input Value | true | ||
| Return Value | None. CLP converts the lat, long information to the street address information, and sends the street address to the requester. | ||
| version | |||
| The protocol version, currently, 1.1.0. | |||
| Required | Yes | ||
| Type | string | ||
| Input Value | The version of the protocol. Example: 1.1.0 | ||
| Return Value | None | ||
{ "host" : "_null_.localdomain", "radio_type" : "unknown", "request_address" : true, "version" : "1.1.0" }
The details of the response are as follows. If a position is successfully determined, the location property is populated with the fields described below; otherwise, it is null.
Location
| Name | Description | Required | Type |
|---|---|---|---|
| latitude | Location’s horizontal distance, north and south of the equator, in degrees (WGS84 datum). | Yes | readonly double |
| longitude | Location’s angular distance, measured east or west from the prime meridian (Greenwich), in degrees (WGS84 datum). | Yes | readonly double |
| accuracy | The range (in meters) in which the Web server or application receives location information. | Yes | readonly double |
| address | The address of the location, if requested and available (see Address Object). | No | object |
Address Object
| Name | Description | Required | Type |
|---|---|---|---|
| region | A state in the US. Example: NY | No | readonly double |
| postal_code | Zip code in the US or postcode in the UK. | No | readonly double |
| street | Name of the street. | No | readonly double |
| street_number | The building's street number. | No | readonly double |
| country_code | Country code (ISO 3166-1). Example: US | No | readonly double |
| country | Name of the Country. Example: USA | No | readonly double |
| city | Name of the city. | No | readonly double |
Example
{ "location":{ "address":{ "region":"NY", "postal_code":"14621", "street":"16 Ilex Pl", "street_number":"", "country_code":"US", "country":"USA", "city":"Rochester" }, "longitude":-77.5883, "latitude":43.1792, "accuracy":2011 } }
In this example, the application attempts to obtain a position from the test CLP by comparing JavaScript code samples from both Clearwire and Google Gears. The code makes use of both these location providers to ascertain that location with the highest accuracy is used by the application. Use this sample to enable location with any Web site.
<html> <head> <title>Google Geolocation API Example doing accuracy comparison</title> <script type="text/javascript" src="./gears_init.js"></script> <script type="text/javascript"> var bestPosition; var call = 0; var locations = new Array(2); //initialize gears try { var geo = google.gears.factory.create('beta.geolocation'); } catch (e) { alert("You need gears from http://gears.google.com ; error= "+ e.toString()); } //callback for position updates function updatePosition(position) { locations[call] = position; call++; alert('call ' + call + ": " + position.latitude + ',' + position.longitude + ' with accuracy of ' + position.accuracy + ' meters.'); //make second call if (call == 1) { callTwo(); return; } if (call == 2) { findBestPosition(); } } function findBestPosition() { if (locations[0] == null && locations[1] == null) { alert('Both attempts to get location failed'); return; } //in case of an error in a call, there will be no position object in array if (locations[0] == null && locations[1] != null) { bestPosition = locations[1]; showBestPosition('Clearwire'); return; } if (locations[1] == null && locations[0] != null) { bestPosition = locations[0]; showBestPosition('Google'); return; } //two calls have been made. compare accuracy from each. if (locations[0].accuracy <= locations[1].accuracy) { bestPosition = locations[0]; showBestPosition('Google'); } else { bestPosition = locations[1]; showBestPosition('Clearwire'); } } function showBestPosition(provider) { //show best location alert('Current lat/lon is: ' + bestPosition.latitude + ',' + bestPosition.longitude + ' with accuracy of ' + bestPosition.accuracy + ' meters.' + ' Provided by ' + provider); //bestPosition is what the web page would then use. } //callback for errors function handleError(positionError) { alert('Attempt to get location failed: ' + positionError.message); call++; //if not on wimax, what does our server send, if anything? I assume it sends a gears error response and that appears to be the case. //make default call in case of error in first call if (call == 1) { callTwo(); } if (call == 2) { findBestPosition(); } } function initLocation() { //first call var providers = ["http://www.google.com/loc/json"]; //the options change parameters in the json request var options = {enableHighAccuracy:true, gearsLocationProviderUrls:providers, gearsRequestAddress:true}; geo.getCurrentPosition(updatePosition, handleError, options); } function callTwo() { //second call var providers = ["http://testlocation.clearwire-wmx.net:8000/json/"]; //the options change parameters in the json request var options = {enableHighAccuracy:true, gearsLocationProviderUrls:providers, gearsRequestAddress:true}; geo.getCurrentPosition(updatePosition, handleError, options); } </script> </head> <body onLoad="initLocation();"> This test page is running a gears query now. Read the alert boxes. </body> </html>
In this example, the application attempts to obtain a position from the test CLP. If the test CLP is unable to provide a position, the Google location service is queried to provide a WiFi or an IP location-based position. You can use this sample to enable location with Google Maps.
<html> <head> <title>Hello World Geolocation</title> <link rel="stylesheet" type="text/css" href="http://code.google.com/apis/gears/samples/sample.css"> </head> <body> <h1>Gears Geolocation Demo</h1> <div id="view-source"> </div> <p id="status"></p> <p><b>This page demonstrates basic usage of the Geolocation API.</b></p> <!-- ====================================== --> <!-- End HTML code. Begin JavaScript code. --> <script type="text/javascript" src="http://code.google.com/apis/gears/gears_init.js"></script> <script type="text/javascript" src="http://code.google.com/apis/gears/samples/sample.js"></script> <script> init(); function init() { if (!window.google || !google.gears) { addStatus('Gears is not installed', 'error'); return; } addStatus('Getting location...'); function successCallback(p) { var address = p.gearsAddress.street + ' ' + p.gearsAddress.city + ', ' + p.gearsAddress.region + ', ' + p.gearsAddress.country + ' (' + p.latitude + ', ' + p.longitude + ')'; clearStatus(); addStatus('Your address is: ' + address); } function errorCallback(err) { var msg = 'Error retrieving your location: ' + err.message; setError(msg); } try { var geolocation = google.gears.factory.create('beta.geolocation'); geolocation.getCurrentPosition( successCallback, errorCallback, { enableHighAccuracy: true, gearsRequestAddress: true,gearsLocationProviderUrls: ["http://testlocation.clearwire-wmx.net:8000/json/", "http://www.google.com/loc/json"]} ); } catch (e) { setError('Error using Geolocation API: ' + e.message); return; } } </script> </body> </html>
Next Topics