×
Namespaces

Variants
Actions
(Difference between revisions)

Calculating the distance between two locations in Symbian Web Runtime

From Nokia Developer Wiki
Jump to: navigation, search
hamishwillee (Talk | contribs)
m (Hamishwillee - Bot change of template (Template:CodeSnippet) - now using Template:ArticleMetaData)
hamishwillee (Talk | contribs)
m (Hamishwillee - Bot update)
Line 1: Line 1:
__NOTOC__
+
{{ArticleMetaData <!-- v1.2 -->
__NOEDITSECTION__
+
{{KBCS}}
+
{{ArticleMetaData
+
|id=CS001175
+
|platform=S60 5th Edition
+
|devices=Nokia 5800 XpressMusic
+
|category=Web Runtime (WRT)
+
|subcategory=S60 Platform Services
+
|creationdate=November 17, 2008
+
|keywords=device.getServiceObject(), Service.Location, Service.Location.GetLocation(), Service.Location.Calculate()
+
 
+
 
|sourcecode= <!-- Link to example source code (e.g. [[Media:The Code Example ZIP.zip]]) -->
 
|sourcecode= <!-- Link to example source code (e.g. [[Media:The Code Example ZIP.zip]]) -->
 
|installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) -->
 
|installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) -->
|sdk=<!-- SDK(s) built and tested against (e.g. [http://linktosdkdownload/ Nokia Qt SDK 1.1]) -->
+
|devices= Nokia 5800 XpressMusic
|devicecompatability=<!-- Compatible devices (e.g.: All* (must have GPS) ) -->
+
|sdk= <!-- SDK(s) built and tested against (e.g. [http://linktosdkdownload/ Nokia Qt SDK 1.1]) -->
|signing=<!-- Empty or one of Self-Signed, DevCert, Manufacturer -->
+
|platform= S60 5th Edition
|capabilities=<!-- Capabilities required (e.g. Location, NetworkServices. -->)
+
|devicecompatability= <!-- Compatible devices (e.g.: All* (must have GPS) ) -->
|author=[[User:Forum Nokia KB]]
+
|dependencies= <!-- Any other/external dependencies e.g.: Google Maps Api v1.0 -->
 +
|signing= <!-- Empty or one of Self-Signed, DevCert, Manufacturer -->
 +
|capabilities= <!-- Capabilities required by the article/code example (e.g. Location, NetworkServices. -->
 +
|keywords= device.getServiceObject(), Service.Location, Service.Location.GetLocation(), Service.Location.Calculate()
 +
|language= <!-- Language category code for non-English topics - e.g. Lang-Chinese -->
 +
|translated-by= <!-- [[User:XXXX]] -->
 +
|translated-from-title= <!-- Title only -->
 +
|translated-from-id= <!-- Id of translated revision -->
 +
|review-by= <!-- After re-review: [[User:username]] -->
 +
|review-timestamp= <!-- After re-review: YYYYMMDD -->
 +
|update-by= <!-- After significant update: [[User:username]]-->
 +
|update-timestamp= <!-- After significant update: YYYYMMDD -->
 +
|creationdate= 20081117
 +
|author= [[User:Nokia Developer KB]]
 +
<!-- The following are not in current metadata -->
 +
|subcategory= S60 Platform Services
 +
|id= CS001175
 
}}
 
}}
  
Line 136: Line 141:
 
==See also==
 
==See also==
  
* [[CS001161 - Obtaining location information]]
+
* [[Obtaining location information using Symbian Web Runtime]]
 
* [http://www.developer.nokia.com/Resources/Library/Web/ Web Developer's Library: JavaScript Location Service API]
 
* [http://www.developer.nokia.com/Resources/Library/Web/ Web Developer's Library: JavaScript Location Service API]
  
[[Category:Symbian Web Runtime]][[Category:S60 Platform Services]][[Category:Code Examples]][[Category:Code Snippet]]
+
[[Category:Symbian Web Runtime]][[Category:S60 Platform Services]][[Category:Code Snippet]][[Category:Code Snippet]]

Revision as of 09:20, 10 May 2012

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

Contents

Overview

This code snippet demonstrates how to calculate the distance between two locations using the Location Service API of Web Runtime (WRT).

Source: widget.xhtml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<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>
</head>
<body>
<div id="bodyContent" class="bodyContent">
<input type="button" value="Display distance"
onclick="displayDistance();" />
</div>
</body>
</html>

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.");
return;
}
 
// 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.");
return;
}
 
// Obtain the location information (asynchronous)
try {
var result = serviceObj.ILocation.GetLocation(criteria,
locationDataReady);
} catch (ex) {
alert(ex);
return;
}
}
 
// 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);
return;
}
 
// 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) {
alert(ex);
distance = undefined;
}
return distance;
}

Postconditions

The distance between two locations is displayed.

See also

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

×