Code Example‎ > ‎

GPS

Choose, File --> new project --> project 
Then select Droidget wizards --> project from New Project dialog  
Fill out Project name field, etc  In the Feature section , Click on Add and select geolocation feature "http://bondi.omtp.org/api/gelocation".  
Then click Finish .
Droidget project with the given name will be created and displayed in the project explorer.




After finished creating project, Bondi Feature <feature name="http://bondi.omtp.org/api/gelocation" /> will be appeared in the config.xml file as follows.

<?xml version="1.0" encoding="UTF-8"?>
<widget id="GPSSample_1279453579736" version="1.0" width="300" height="150">
    <content src="index.html" type="text/html"/>
    <name>GPSSample</name>
    <description>Sample Widget with GPS</description>
    <author url="http://www.gclue.jp">GClue</author>
    <icon src="icon.png"/>
    <license></license>
    <feature name="http://bondi.omtp.org/api/geolocation" />
</widget>

Create JavaScript File

create a new folder called js and under that folder create a new file and name it as "script.js".
Then add the following functions.

js/script.js
function buttonHandler(){
    document.getElementById("divMsg").innerHTML = "Searching";  
    bondi.geolocation.getCurrentPosition(onPositionSuccess, onPositionError, {timeout:40000});
}

// this function will handle each position updates
function onPositionSuccess(position) {
    lat = position.coords.latitude;
    lon = position.coords.longitude;
    document.getElementById("divMsg").innerHTML = "lat:" + lat + "<br>" + "lon:" + lon;  
}

// this function will handle each error in position updates
function onPositionError(error) {
    document.getElementById("divMsg").innerHTML = "Error";
}

Edit index.html

Change the index.html file content as follows. 

index.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta http-equiv="Content-Style-Type" content="text/css" />
    <meta http-equiv="Content-Script-Type" content="text/javascript" />
    <title>GPS Sample</title>
    <script type="text/javascript" src="js/script.js"></script>
</head>
<body>
    <div id="divMsg" style="color: yellow; height: 3.0em; font-size: 1em; "></div>
    <input type="button" id="formBtn" value="Get GPS" onclick="buttonHandler()"/>
</body>
</html>

Packaging

Selecte the project from Project Explorer and Click Droidget>>Packaging

Widget Installation

Now save and deploy the project
  • From the eclipse menu select Droidget --> packaging
  • Then Droidget-->install packaging
Or 
  • upload the Widget to web server and update the download.html file as follows. Then access the download.html from the smart phone and install the widget from URI.

Download.html
<html>
<head>
    <title>Download</title>
</head>
<body>
    <a href="droidget://www.example.com/GPSSample.wgt">GPSSample</a><br>
</body>
</html>

Output

after installation , output will be like this. 


Comments