How to access GPS location using S60 5th Edition API
In the application showed in this post you can see how to develop a location based rss reader using WRT with the position of the device (using the new S60 5th edition WRT API).
First of all, let’s see the problem:
Think you’re in an environment where news are provided as a rss file. This news could be an advertisement, an event, a shop or anything else you want.
Think also you are using a mobile phone with a RSS reader inside this environment and there is a large amount of news (a big rss file) about this environment, but you’re only interested in the news nearest your location.
This situation is showed in the picture below. You’ll get information (news) about sector C, even you’re in the sector A of the shopping center.
This situation becomes a problem to the user when the amount of information about the environment (the shopping center) is too large (sector A + sector B + sector C).
This problem can be fixed if the rss reader of the mobile phone retrieves only information (news) about the sector where the user is inside it. This can be done attributing a position (latitude and longitude) for each ‘item’ of the ‘rss’ file of the environment, as showed in the following picture.
So, the user has to send his position in the request of the rss file to get only information about the sector where he is inside it.
For example, if the user is inside the sector A, he’ll get only information about item 1 and 2, but not about item 3,4,5 or 6. So, we can realize that only the shops who item 1 and 2 represent are inside the sector A.
An example of the development of a RSS server with filtered content is showed in this post.
Ok, now we have a RSS Server that filter the rss content based in the location given as parameter in the request. But, we need a mobile client to send the location of the device and read the filtered RSS content.
In the WRT API for 5th edition S60, the developer can get the GPS position using the new Service class. This class access the GPS position (and many other resources) of the mobile phone and retrieve a lot of information about location. Below it’s showed a code example of how to get this information and send the request to the RSS Server of the previous picture.
var so; // Called from onload() function setupLocation() { try { //Retrieves the Service object to the ILocation interface so = device.getServiceObject("Service.Location", "ILocation"); } catch (e) { alert(' ' +e); } } // Get Location 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 send the GPS position to the server function callbackLocation(transId, eventCode, result){ var latitude = result.ReturnValue.Latitude; var longitude = result.ReturnValue.Longitude; //Sends the position latitude and longitude to the server using the XMLHttpRequest class. var req = null; try { req = new XMLHttpRequest(); // Make sure that the browser supports overrideMimeType if (typeof req.overrideMimeType != "undefined") { req.overrideMimeType("text/xml"); } req.onreadystatechange = function() { // Request states are 0 through 4, where 4 equals complete if (req.readyState == 4) { // Server returns numeric code 200 for "OK". if (req.status == 200) { //Handling the retrieved content from the server. ... } } else { alert("There was a problem retrieving the RSS file."); } } // is the server URL to retrieve the RSS filtered file. var url = "?d="+date.getMilliseconds()+"&latitude="+latitude+"&longitude="+longitude; req.open(url); req.send(null); } catch (ex) { alert(ex); } }
The application also show a static map with the location of the user and the location of each retrieved item.


Leave a comment