×
Namespaces

Variants
Actions
(Difference between revisions)

How to get GPS position with WRT

From Nokia Developer Wiki
Jump to: navigation, search
hamishwillee (Talk | contribs)
m (Hamishwillee - Bot update - Add ArticleMetaData)
hamishwillee (Talk | contribs)
m (Hamishwillee - Fix categories)
 
Line 1: Line 1:
 +
[[Category:Symbian Web Runtime]][[Category:Code Snippet]][[Category:Location]]
 
{{ArticleMetaData <!-- v1.2 -->
 
{{ArticleMetaData <!-- v1.2 -->
 
|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]] -->
Line 147: Line 148:
 
</html>
 
</html>
 
</code>
 
</code>
 
[[Category:How To]]
 
[[Category:Symbian Web Runtime]]
 
[[Category:Code Snippet]]
 

Latest revision as of 09:06, 10 October 2012

Article Metadata
Article
Created: mfabiop (30 Nov 2008)
Last edited: hamishwillee (10 Oct 2012)

[edit] How to get the GPS position

In S60 5th edition, the developer can get the GPS position using WRT API. This code snippet illustrates how to do this.

var so;
 
try {
//Retrieves the Service object to the ILocation interface
so = device.getServiceObject("Service.Location", "ILocation");
} catch (e) {
alert(' ' +e);
}
 
 
// Gets the GPS position
function getLocationAsync() {
// This specifies update option used while retrieving location estimation.
var updateoptions = new Object();
// Setting PartialUpdates to 'FALSE' ensures that user get atleast
// BasicLocationInformation (Longitude, Lattitude, and Altitude.) is the default when no LocationInformationClass criteria is given.
updateoptions.PartialUpdates = false;
 
var criteria = new Object();
 
criteria.Updateoptions = updateoptions;
 
try {
//Executes the GetLocation method and sets the callbackLocation as the callback function to be called.
so.ILocation.GetLocation(criteria,callbackLocation);
} catch (e) {
alert ("getLocationAsync: " + e);
}
}
 
//Callback function that receives the result as parameter.
function callbackLocation(transId, eventCode, result){
var latitude = result.ReturnValue.Latitude;
var longitude = result.ReturnValue.Longitude;
}

[edit] An example using the device position

The developer can use the latitude and longitude to render a map with the device position. In this article it was shown how the user can develop a new UI component and a Map component was shown as an example.

The developer can send the latitude and longitude retrieved from the WRT API and render a static map with the device position using this post. The code is shown below.

var so;
 
var uiManager;
 
var mapControl;
 
 
 
 
// Gets the GPS position
function getLocationAsync() {
try {
//Retrieves the Service object to the ILocation interface
so = device.getServiceObject("Service.Location", "ILocation");
} catch (e) {
alert(' ' +e);
}
// This specifies update option used while retrieving location estimation.
var updateoptions = new Object();
// Setting PartialUpdates to 'FALSE' ensures that user get atleast
// BasicLocationInformation (Longitude, Lattitude, and Altitude.) is the default when no LocationInformationClass criteria is given.
updateoptions.PartialUpdates = false;
 
var criteria = new Object();
 
criteria.Updateoptions = updateoptions;
 
try {
//Executes the GetLocation method and sets the callbackLocation as the callback function to be called.
so.ILocation.GetLocation(criteria,callbackLocation);
} catch (e) {
alert ("getLocationAsync: " + e);
}
}
 
//Callback function that receives the result as parameter.
function callbackLocation(transId, eventCode, result){
var latitude = result.ReturnValue.Latitude;
var longitude = result.ReturnValue.Longitude;
if(latitude != null && longitude != null){
mapControl.setCenter(latitude,longitude);
mapControl.reloadMap("You're Here!");
}
}
 
function loadLatLng(event){
getLocationAsync();
}
 
function setup(){
uiManager = new UIManager();
var listView = new ListView(null, "List");
var renderMapBTN = new FormButton(null, "Render map");
renderMapBTN.addEventListener("ActionPerformed",loadLatLng);
 
mapControl = new Map("Map","",null,null);
listView.addControl(renderMapBTN);
listView.addControl(mapControl);
uiManager.setView(listView);
}

This is the main.html file, remember to copy the Map.js file from this article.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<script type="text/javascript" src="WRTKit/WRTKit.js"></script>
<script type="text/javascript" src="js/Map.js"></script>
<script type="text/javascript" src="js/mapUi.js"></script>
</head>
<body onload="setup()" style=width:100%;height:100%;>
</body>
</html>
This page was last modified on 10 October 2012, at 09:06.
57 page views in the last 30 days.
×