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. Thanks for all your past and future contributions.

Calculating the distance between two locations in Symbian Web Runtime

From Wiki
Jump to: navigation, search

This code snippet demonstrates how to calculate the distance between two locations on Symbian Web Runtime using the Location Service API.

Article Metadata
Tested with
Devices(s): Nokia 5800 XpressMusic
Platform(s): S60 5th Edition
Keywords: device.getServiceObject(), Service.Location, Service.Location.GetLocation(), Service.Location.Calculate()
Created: User:Nokia Developer KB (17 Nov 2008)
Last edited: hamishwillee (05 Oct 2012)


Source: widget.xhtml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
<html xmlns="">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<link rel="StyleSheet" href="style/general.css" type="text/css" />
<script type="text/javascript" src="script/script.js" />
<title>WRT Application</title>
<div id="bodyContent" class="bodyContent">
<input type="button" value="Display distance"
onclick="displayDistance();" />

Source: script.js

var serviceObj = null;
var distanceCriteria = null;
window.onload = init;
// Initializes the widget
function init() {
// Obtain the Location service object
try {
serviceObj = device.getServiceObject("Service.Location", "ILocation");
} catch (ex) {
alert("Service object cannot be found.");
// Initialize the criteria for the service object
criteria = new Object();
criteria.LocationInformationClass = "BasicLocationInformation";
distanceCriteria = new Object();
distanceCriteria.MathRequest = "FindDistance";
function displayDistance() {
if (serviceObj == null) {
alert("Service object has not been initialized.");
// Obtain the location information (asynchronous)
try {
var result = serviceObj.ILocation.GetLocation(criteria,
} catch (ex) {
// Called when the location data has been obtained
function locationDataReady(transId, eventCode, result) {
// On error situation, display the error message
if (eventCode == 4) {
alert("Error " + result.ErrorCode + ": " + result.ErrorMessage);
// The source location is where the device now is
var srcLocation = new Object();
srcLocation.Latitude = result.ReturnValue.Latitude;
srcLocation.Longitude = result.ReturnValue.Longitude;
srcLocation.Altitude = result.ReturnValue.Altitude;
// The destination location is initialized to specific coordinates
var destLocation = new Object();
destLocation.Latitude = 61.0;
destLocation.Longitude = 24.0;
destLocation.Altitude = 0;
// Calculate the distance between the source and the destination
var distance = calculateDistance(srcLocation, destLocation);
// Display the distance rounded to two decimals
alert(distance.toFixed(2) + " m");
// Calculates the distance from the source location to the destination location
function calculateDistance(srcLocation, destLocation) {
var distance = 0.0;
distanceCriteria.DistanceParamSource = srcLocation;
distanceCriteria.DistanceParamDestination = destLocation;
try {
var distResult = serviceObj.ILocation.Calculate(distanceCriteria);
distance = distResult.ReturnValue;
} catch (ex) {
distance = undefined;
return distance;


The distance between two locations is displayed.

See also

This page was last modified on 5 October 2012, at 06:34.
43 page views in the last 30 days.