Namespaces
Variants
Actions

How to get GPS position with WRT

Jump to: navigation, search

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;
}

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>

Comments

(no comments yet)

This page was last modified on 19 May 2011, at 03:44.
79 page views in the last 30 days.
Nokia Developer aims to help you create apps and publish them so you can connect with users around the world.

京ICP备05048969号  © Copyright Nokia 2012 All rights reserved