3. BlackBerry Location API
Since Device Software version 4.1, the BlackBerry browser
has included a proprietary blackberry.location object. To determine
whether a BlackBerry device has GPS support, we should check the Boolean
property blackberry.location.GPSSupported.
The best way to get the user’s location is to use the onLocationUpdate(callback, stringCallback)
method, which receives a callback function as the first parameter and a
string for callback information as the second.
Warning:
In BlackBerry software before 4.6 the callback method in
onLocationUpdate must be passed as
a string.
The removeLocationUpdate(callback) method will
remove the callback, and refreshLocation requests an update of
the location by calling the previously defined onLocationUpdate callback
method.
Once we receive a callback call, we can read the position using
blackberry.location.latitude and
blackberry.location.longitude.
We can define the method to obtain the GPS location using blackberry.location.setAidMode(mode), using
one of the following modes:
Cellsite (mode 0), the
fastest and least accurate mode
Assisted (mode 1), using
some kind of A-GPS
Autonomous (mode 2), using
only GPS
Therefore, for tracking the user’s location, the following sample
will be useful:
if (blackberry.location!= undefined) {
// It's a Blackberry with Location support
blackberry.location.onLocationUpdate(userLocated);
}
function userLocated() {
var latitude = blackberry.location.latitude;
var longitude = blackberry.location.longitude;
var timeOfLocation = blackberry.location.timestamp;
}
4. Widget APIs
Almost all widget APIs have geolocation support from
JavaScript. Remember that in these cases the user will not be using the
browser explicitly.
5. GSMA OneAPI
OneAPI is a cross-operator API organized by the GSM
Association. At the time of the writing of this book, the API is still
under development, and fewer than 10 operators worldwide are connected.
With this API we can access the user’s location from our servers, using
his phone number. The website to register as a developer and obtain a
token to access the OpenAPI web services is http://oneapi.aepona.com. The API supports SOAP Web
Services and REST using HTTP.
One of the services supported by OpenAPI is geolocation. With the
Location API, we can get a user’s longitude and latitude using the
mobile operator’s cells’ positions. To use the API we need to get a key
from the website. Then, if we want to use REST, we can create an HTTP
request to a URL like the following:
https://developer.aepona.com/TerminalLocationService/Proxy/REST/<key>?address=
tel:<tel>&accuracy=coarse
where <key> is the key assigned
to our developer account and <tel> is
the international number of the phone we want to geolocate. If the
request is successful, we will receive a response like the
following:<response timestamp="2010-06-06T12:31:07.014Z" longitude="10.22244"
latitude="54.601505" altitude="10.0" accuracy="200"/>
The list of supported operators is on the website, and the goal is
to have all the operators worldwide using the same API.
Note:
Loki.com offers an API for developers based on a geolocation
plug-in for desktop browsers and an IP Geolocation service that can be
used from a mobile device.
6. Multiplatform Geolocation API
geo-location-javascript is a
multiplatform framework designed for mobile browsers. It is available as
an open source project hosted in Google Code (http://code.google.com/p/geo-location-javascript/).
The framework is compatible with the iPhone and other devices that
use the W3C Geolocation API, devices that use the Google Gears
Geolocation API (including Android and Windows Mobile devices), and
BlackBerry devices. It also works with the Nokia Web Runtime widget
engine, Palm Pre for webOS, and other browsers with less market
share.
This framework allows us to use the same code for all platforms.
To use the framework, we just need to download the JavaScript file, host
it on our servers, and include it in our websites:
<script src="http://code.google.com/apis/gears/gears_init.js"
type="text/javascript"></script>
<script src="geo.js" type="text/javascript"></script>
Warning:
To support Gears on compatible devices, we must insert the
gears_init script in the HTML document. We can
create a function that inserts this script only on compatible devices
using DOM.
The API creates a global variable called geo_position_js with an init method that returns a Boolean indicating
whether the device is compatible with geolocation.
Once we are sure that geolocation is available we should call
getCurrentPosition, passing two
callbacks (the position handler and the error handler):
if (geo_position_js.init()){
geo_position_js.getCurrentPosition(userLocated, locationError);
} else{
alert("GeoLocation not available");
}
The callback parameters are aligned with the W3C API, so we can
use the same handlers:
function userLocated(position) {
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
var timeOfLocation = position.timestamp;
}
function locationError(error) {
alert(error.code);
}
If the real implementation supports other options (like altitude), we can read them in the callback
parameter.
6.1. Simulating movement
To assist us in development phase, the framework also allows us
to simulate users moving without a real device actually moving. To
simulate movement we should insert a second script:
<script src="js/geo_position_js_simulator.js" type="text/javascript"
charset="utf-8"></script>
Then, we can create an array of locations and initiate the
simulation:
var simulation=new Array();
// These are Barcelona's positions from the simulator sample
simulation.push({ coords:{latitude:41.399856290690956,
longitude:2.1961069107055664}, duration:5000 });
simulation.push({ coords:{latitude:41.400634242252046,
longitude:2.1971797943115234}, duration:5000 });
simulation.push({ coords:{latitude:41.40124586762545,
longitude:2.197995185852051}, duration:5000 });
// Initiate the simulation
geo_position_js_simulator.init(simulation);
Warning:
The framework does not support a tracking system, so if we
want to use it to track users’ movements we should implement a
setTimeout or setInterval timer to call getCurrentPosition frequently.
7. IP Geolocation
There are a lot of free and commercial IP address
geolocation services available for use on our servers. When using such a
solution, we need to remember that a BlackBerry can browse through a
corporate network, so the IP address will be the network IP address and
not the user’s. The same applies to proxied browsers like Opera
Mini.
7.1. Reading the IP address
We can read the IP address from the host using the
appropriate mechanism for the server platform. For example, in PHP we
read the address using:
$IP = $_SERVER['REMOTE_ADDR'];
However, we must remember that this IP address may belong to a
renderer proxy. For example, when an Opera Mini user accesses our
website the IP address will be always the same, because the client
contacting our server is actually the Opera Mini server. Fortunately,
Opera Mini servers offer us another HTTP header that provides the
actual IP address of the requesting mobile device: the X-Forwarded-For header contains a CSV list
of the IP addresses of all the proxy servers the request has passed
through on its way from the device to the Mini proxy. The last IP
address will be the address of the original requestor (the mobile
device).
Once we have the IP address to query, we can use a web service
to get the country/city details, or download the Geo-IP open source
database from http://software77.net/geo-ip.
Warning:
We need to keep in mind that IP geocoding is useful only to
get the user’s country for devices connected to the Internet via
2.5G or 3G, because what we’ll receive is the operator’s gateway IP
address. If the user is using WiFi, depending on the zone, we can
usually get more accurate details.
7.2. Google’s ClientLocation object
Google provides a set of Ajax APIs (Maps, Search, etc.)
that can be used to create feature-rich dynamic websites. Whenever one
of these APIs is loaded on a client, the Ajax API loader attempts to
geolocate the user using the device’s IP address.
To use the Ajax APIs, we need to get an API key, freely
available from the website http://code.google.com/apis/ajaxsearch/signup.html.
Once we have an API key, we need to insert this script:
<script type="text/javascript"
src="http://www.google.com/jsapi?key={key}"></script>
To use the client location feature, we must then load an API.
For example:
<script type="text/javascript">
google.load("search", "1");
</script>
Once we’ve loaded the API, the google.loader.ClientLocation object will be
populated with properties like the following:
latitude
longitude
address.city
address.country
address.country_code
address.region
Warning:
This technique works only on compatible devices and should be
used only if we are going to make use of one of the Ajax APIs (Ajax
Search, Maps, Ajax Feeds, Earth, Data, Visualization, Friend
Connect, or Ajax Language).