Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

Revision as of 09:14, 24 June 2011 by hamishwillee (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Acquiring a cached location value in WRT

From Wiki
Jump to: navigation, search

Article Metadata
Tested with
Devices(s): Nokia N97, Nokia N97 Mini
Platform(s): S60 5th Edition, S60 3rd FP2
Keywords: location javascript
Created: petrosoi (15 Oct 2009)
Last edited: hamishwillee (24 Jun 2011)


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

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"
<html xmlns="">
<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" />
var locAPI = null;
function init(){
locAPI = device.getServiceObject("Service.Location", "ILocation");
function getLastKnownPosition(){
// 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
myLogger('ErrorCode: ' + result.ErrorCode + '<br/>' + 'ErrorMessage: ' + result.ErrorMessage);
function myLogger(txt){
document.getElementById("myconsole").innerHTML = txt;
<body onload="init();">
<div><a style="font-size:1.5em" id="actionjackson" href="#" onclick="getLastKnownPosition();">Last known position</a></div>
<div id="myconsole"></div>
This page was last modified on 24 June 2011, at 09:14.
34 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.


Thank you!

We appreciate your feedback.