How to Monitor Geographic Location Updates in WRT Widget
Article Metadata
Code Example
Installation file: Media:Geolocation_test.zip
Tested with
Devices(s): Nokia N97
Article
Keywords: watchPosition(), geolocation, Platform Service 2.0
Created: vasant21
(16 Jan 2010)
Last edited: hamishwillee
(05 Jul 2012)
Contents |
Overview
- This article and code snippet below explains how one can monitor changes in Geographic Location in a Widget using geolocation service object in Nokia Platform Services 2.0.
- Before we start with the article, there are two ways of including Platform Services 2.0 support to the widget:
- Installing platformservices_v2_0_beta.SIS, will add the JavaScript library to a common location (c:\system\widgetlibs\platformservices.js) that can be accessed by any widget.
- Including the JavaScript library platformservices.js directly in a widget.
- We will use the second option for simplicity, and ease of testing the widget.
- In the code below we try to monitor the changes in the device geographic location and display it on screen, for that we will use watchPosition api.
Example
<html>
<head>
<script type="text/javascript" src="platformservices.js" charset= "utf-8" />
<script language="javascript">
var so = null;
var tid = ""
// Callback function called whenever change in location is detected.
function onLocationUpdate( newLocation ){
var lon = newLocation.coords.longitude;
var lat = newLocation.coords.latitude;
alert("Location Changed : \n Longitude: " + lon + "\n Latitude: " + lat);
};
//Error handling callback function for watchPosition.
function onLocationError( error ){
alert("Error getting Location Updates: " + error.message );
};
// On exit lets just clear the ongoing watch for location changes if any.
window.widget.onexit = function() {
if( tid ){
so.clearWatch(watchId);
}
};
try{
// Lets first create geolocation object.
so = com.nokia.device.load("", "com.nokia.device.geolocation");
// Lets start getting location updates.
var tid = so.watchPosition(onLocationUpdate, onLocationError);
}catch(e){
alert("Error : " + e);
};
</script>
</head>
<body>Location Updates Test Widget</body>
</html>
Sample Widget
Media:Geolocation_test.zip is a sample widget - install and launch.
How to Test
- Launch the widget, it will start monitoring location updates and will display the location if changes are detected in the device location.


(no comments yet)