Acquiring a cached location value in WRT
Article Metadata
Tested with
Compatibility
Article
Overview
Latest S60 devices incorporate a small but valuable addition to JavaScript Location Service API.
"Last known position" information is useful for getting a quick response from the location API on where the last positioning fix was acquired.
As a developer, you might want to first check if this cached value is available and ask the user if he's happy with the estimate, before requesting a new location fix from the framework.
I put together some sample code to assist you in integrating this feature to your widget.
The beef
Set up the location service object and call:
var result = locAPI.ILocation.GetLastPosition();
If the returned error code is 0, you can use the freshly acquired cached location data:
if (result.ErrorCode == 0){
var posObject = result.ReturnValue;
myLogger(posObject.Latitude + ' - ' + posObject.Longitude);
}
If the "last known position" feature is supported, but no cached data is available:
else if (result.ErrorCode == 1014){
myLogger('Last known position data is not available');
}
...and finally, if your user is sporting old SW, that does not support this feature
else{
myLogger('ErrorCode: ' + result.ErrorCode + '<br/>' + 'ErrorMessage: ' + result.ErrorMessage);
}
Devices that currently incorporate this feature
N97 20.0.019 and later N97 Mini 10.0.020 and later
Full example code
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title> Sample Widget</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script language="javascript" type="text/javascript" src="basic.js"></script>
<link rel="stylesheet" href="basic.css" type="text/css">
<META NAME="Generator" CONTENT="Nokia WRT plug-in for Aptana Studio 2.2.0" />
<script>
var locAPI = null;
function init(){
locAPI = device.getServiceObject("Service.Location", "ILocation");
}
function getLastKnownPosition(){
if(!locAPI)
init();
// Here's the fancy (but hidden) API call
var result = locAPI.ILocation.GetLastPosition();
// Yey, we can use the cached location info!
if (result.ErrorCode == 0){
var posObject = result.ReturnValue;
myLogger(posObject.Latitude + ' - ' + posObject.Longitude);
}
// Last known position data is not available
else if (result.ErrorCode == 1014){
myLogger('Last known position data is not available');
}
// Hmpf, this SW version does not support last known position API
else{
myLogger('ErrorCode: ' + result.ErrorCode + '<br/>' + 'ErrorMessage: ' + result.ErrorMessage);
}
}
function myLogger(txt){
document.getElementById("myconsole").innerHTML = txt;
}
</script>
</head>
<body onload="init();">
<div><a style="font-size:1.5em" id="actionjackson" href="#" onclick="getLastKnownPosition();">Last known position</a></div>
<div id="myconsole"></div>
</body>
</html>

