MOBILE

Programming the Mobile Web : Geolocation and Maps - Detecting the Location (part 3)

2/8/2011 4:21:55 PM

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).


Other  
  •  Programming the Mobile Web : Geolocation and Maps - Location Techniques
  •  iPhone Programming : Table-View-Based Applications - Connecting the Controller to the Model
  •  Programming the Mobile Web : Mobilizing WordPress and Other CMSs
  •  Programming the Mobile Web : Server-Side Browser Detection and Content Delivery - Content Adaptation
  •  Programming the Mobile Web : Multimedia and Streaming
  •  Mobile Application Security : BlackBerry Security - Development and Security Testing
  •  Mobile Application Security : BlackBerry Security - Introduction to Platform
  •  Windows Phone 7 Development : Using a WebBrowser Control to Display Dynamic Content
  •  Windows Phone 7 Development : Using a WebBrowser Control to Display Local HTML Content
  •  Windows Mobile Security - Networking
  •  Windows Mobile Security - Local Data Storage
  •  Windows Mobile Security - Permissions and User Controls
  •  Windows Phone 7 Development : Using a WebBrowser Control to Display Web Content
  •  Windows Phone 7 Development : Adding a WebBrowser Control
  •  Programming the Mobile Web : Content Delivery (part 3)
  •  Programming the Mobile Web : Content Delivery (part 2) - File Delivery
  •  Programming the Mobile Web : Content Delivery (part 1) - Defining MIME Types
  •  iPhone Application Development : Using Switches, Segmented Controls, and Web Views (part 3)
  •  iPhone Application Development : Using Switches, Segmented Controls, and Web Views (part 2)
  •  iPhone Application Development : Using Switches, Segmented Controls, and Web Views (part 1)
  •  
    Top 10
    Nikon 1 J2 With Stylish Design And Dependable Image And Video Quality
    Canon Powershot D20 - Super-Durable Waterproof Camera
    Fujifilm Finepix F800EXR – Another Excellent EXR
    Sony NEX-6 – The Best Compact Camera
    Teufel Cubycon 2 – An Excellent All-In-One For Films
    Dell S2740L - A Beautifully Crafted 27-inch IPS Monitor
    Philips 55PFL6007T With Fantastic Picture Quality
    Philips Gioco 278G4 – An Excellent 27-inch Screen
    Sony VPL-HW50ES – Sony’s Best Home Cinema Projector
    Windows Vista : Installing and Running Applications - Launching Applications
    Most View
    Bamboo Splash - Powerful Specs And Friendly Interface
    Powered By Windows (Part 2) - Toshiba Satellite U840 Series, Philips E248C3 MODA Lightframe Monitor & HP Envy Spectre 14
    MSI X79A-GD65 8D - Power without the Cost
    Canon EOS M With Wonderful Touchscreen Interface (Part 1)
    Windows Server 2003 : Building an Active Directory Structure (part 1) - The First Domain
    Personalize Your iPhone Case
    Speed ​​up browsing with a faster DNS
    Using and Configuring Public Folder Sharing
    Extending the Real-Time Communications Functionality of Exchange Server 2007 : Installing OCS 2007 (part 1)
    Google, privacy & you (Part 1)
    iPhone Application Development : Making Multivalue Choices with Pickers - Understanding Pickers
    Microsoft Surface With Windows RT - Truly A Unique Tablet
    Network Configuration & Troubleshooting (Part 1)
    Panasonic Lumix GH3 – The Fastest Touchscreen-Camera (Part 2)
    Programming Microsoft SQL Server 2005 : FOR XML Commands (part 3) - OPENXML Enhancements in SQL Server 2005
    Exchange Server 2010 : Track Exchange Performance (part 2) - Test the Performance Limitations in a Lab
    Extra Network Hardware Round-Up (Part 2) - NAS Drives, Media Center Extenders & Games Consoles
    Windows Server 2003 : Planning a Host Name Resolution Strategy - Understanding Name Resolution Requirements
    Google’s Data Liberation Front (Part 2)
    Datacolor SpyderLensCal (Part 1)