org.symbian.tools.wrttools.doc.WebDeveloper/html/GUID-620340F8-F844-445E-80AA-71C757320B7E.html
changeset 229 716254ccbcc0
equal deleted inserted replaced
228:913c9751c067 229:716254ccbcc0
       
     1 
       
     2 <!DOCTYPE html
       
     3   PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
       
     4 <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/><meta name="copyright" content="(C) Copyright 2009"/><meta name="DC.rights.owner" content="(C) Copyright 2009"/><meta name="DC.Type" content="mobileconcept"/><meta name="DC.Title" content="Location Service API example"/><meta name="DC.Relation" scheme="URI" content="GUID-DEA4164E-5F03-4B57-97B9-4C6238481F89"/><meta name="DC.Relation" scheme="URI" content="GUID-7C69DDA4-16F1-4A8F-BDB2-4CB0015B4E81"/><meta name="DC.Relation" scheme="URI" content="GUID-65AAF569-D347-462B-B59A-9D7CA184AB9C"/><meta name="DC.Relation" scheme="URI" content="GUID-53CE4DE6-F065-4339-8C18-5C30A9540053"/><meta name="DC.Format" content="XHTML"/><meta name="DC.Identifier" content="GUID-620340F8-F844-445E-80AA-71C757320B7E"/><title>Location Service API example </title><script type="text/javascript">
       
     5       function initPage() {}
       
     6     </script><link href="../PRODUCT_PLUGIN/book.css" rel="stylesheet" type="text/css"/><link href="css/s60/style.css" rel="stylesheet" type="text/css" media="all"/></head><body onload="initPage();"><div class="body"><div class="contentLeft prTxt"><h1 class="pageHeading" id="GUID-620340F8-F844-445E-80AA-71C757320B7E">Location Service API example</h1><div>
       
     7 <p/>
       
     8 <p>This section presents the full source code of a working sample widget
       
     9 for the <a href="GUID-4DDE31C7-EC0D-4EEC-BC3A-A0B0351154F8.html#GUID-4DDE31C7-EC0D-4EEC-BC3A-A0B0351154F8">Location Service</a>.
       
    10 You can download the <code>wgz</code> package for this widget from
       
    11 section <a href="GUID-775005BC-2FF8-45A9-BBA6-6CED6B5780A2.html#GUID-775005BC-2FF8-45A9-BBA6-6CED6B5780A2">Example widgets</a>.</p>
       
    12 <p>For general information about creating widgets, see section <a href="GUID-0E3095DB-03FF-4240-83F2-6D876AD2083A.html#GUID-0E3095DB-03FF-4240-83F2-6D876AD2083A">Widget component files</a>.</p>
       
    13 <p>For widget development and debugging purposes, this example writes its
       
    14 output to <code>c:\data\jslog_widget.log</code> using <code>console.info</code>.
       
    15 For instructions on how to enable logging in the Web browser for S60, see
       
    16 section <a href="GUID-B584CA90-543B-4AED-B134-A3A616259DB9.html#GUID-B584CA90-543B-4AED-B134-A3A616259DB9">JavaScript console</a>.</p>
       
    17 <div><h3>Info.plist</h3>
       
    18 <pre class="codeblock" id="GUID-E8B65BD0-1F31-48F9-B8BE-91706A37AAB2">&lt;?xml version="1.0" encoding="UTF-8"?&gt;
       
    19 &lt;!DOCTYPE plist PUBLIC "-//Nokia//DTD PLIST 1.0//EN" "http://www.nokia.com/NOKIA_COM_1/DTDs/plist-1.0.dtd"&gt;
       
    20 &lt;plist version="1.0"&gt;
       
    21 &lt;dict&gt;
       
    22   &lt;key&gt;DisplayName&lt;/key&gt;
       
    23   &lt;string&gt;LocationSample&lt;/string&gt;
       
    24   &lt;key&gt;Identifier&lt;/key&gt;
       
    25   &lt;string&gt;com.nokia.widget.sapi.contact.sample&lt;/string&gt;
       
    26   &lt;key&gt;Version&lt;/key&gt;
       
    27   &lt;string&gt;1.0&lt;/string&gt;
       
    28   &lt;key&gt;MainHTML&lt;/key&gt;
       
    29   &lt;string&gt;location-sample.html&lt;/string&gt;
       
    30 &lt;/dict&gt;
       
    31 &lt;/plist&gt;
       
    32 </pre>
       
    33 </div>
       
    34 <div><h3>location-sample.html</h3>
       
    35 <pre class="codeblock" id="GUID-C0BCD1CE-D35B-49DC-8856-5044E548001E">&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"&gt;
       
    36 &lt;html&gt;
       
    37   &lt;head&gt;
       
    38     &lt;script type="text/javascript" src="js/common.js"&gt;&lt;/script&gt;
       
    39     &lt;script type="text/javascript" src="js/location-sample.js"&gt;&lt;/script&gt;
       
    40   &lt;/head&gt;
       
    41   &lt;body id='docBody' bgcolor="#ddeeff" onload="setup()" style=width:100%;height:100%;&gt;
       
    42 
       
    43     &lt;form name="frm"&gt;
       
    44       &lt;h3&gt;Location API Sample Widget&lt;/h3&gt;
       
    45 
       
    46       &lt;input type="button" onclick="getLocation('img1')" value="GetLocation"&gt;&lt;img id="img1" src="pic/blank.png" width="25" height="25" align="center"&gt;&lt;br&gt;
       
    47       &lt;input type="button" onclick="getLocationAsync('img2')" value="GetLocationAsync"&gt;&lt;img id="img2" src="pic/blank.png" width="25" height="25" align="center"&gt;&lt;br&gt;
       
    48       &lt;input type="button" onclick="traceLocation('img3')" value="Trace"&gt;&lt;img id="img3" src="pic/blank.png" width="25" height="25" align="center"&gt;&lt;br&gt;
       
    49       &lt;input type="button" onclick="cancelNotification('img4')" value="CancelNotification"&gt;&lt;img id="img4" src="pic/blank.png" width="25" height="25" align="center"&gt;&lt;br&gt;
       
    50       &lt;input type="button" onclick="calculateLocation('img5')" value="Calculate"&gt;&lt;img id="img5" src="pic/blank.png" width="25" height="25" align="center"&gt;&lt;br&gt;
       
    51 
       
    52       &lt;hr&gt;
       
    53 
       
    54       &lt;div class='locationsample' id='locationsample' bgcolor="#ddeeff" style=width:100%;height:100%;overflow:auto&gt;&lt;/div&gt;
       
    55 
       
    56     &lt;/form&gt;
       
    57 
       
    58   &lt;/body&gt;
       
    59 &lt;/html&gt;
       
    60 </pre>
       
    61 </div>
       
    62 <div><h3>common.js</h3>
       
    63 <pre class="codeblock" id="GUID-E3179BD2-802C-4663-BCEA-2A58D50E980E">// common.js
       
    64 //
       
    65 // This file contains some utility functions
       
    66 
       
    67 // Check the error code and show the information to users
       
    68 function checkError(message, resultList, divId, imgId)
       
    69 {
       
    70   var errCode = resultList.ErrorCode;
       
    71   var msg = "";
       
    72 
       
    73   if (errCode) {
       
    74     msg = message + "&lt;BR&gt;" + "Failed Error: " + errCode + "&lt;BR&gt;";
       
    75     if(resultList.ErrorMessage != undefined)
       
    76       msg += "Error Message: " + resultList.ErrorMessage;
       
    77     showIMG(imgId,"no"); 
       
    78   } else {
       
    79     showIMG(imgId,"yes"); 
       
    80   }
       
    81 
       
    82   //print error message
       
    83   if(divId != null &amp;&amp; divId != undefined)
       
    84     document.getElementById(divId).innerHTML = msg;
       
    85   console.info(msg);
       
    86 
       
    87   return errCode;
       
    88 }
       
    89 
       
    90 // Build the message by reading a iteratorable list in a recursive manner
       
    91 function showIterableList(iterator)
       
    92 {
       
    93   var msg = "";
       
    94   try
       
    95   {
       
    96     iterator.reset();
       
    97     var item;
       
    98     while (( item = iterator.getNext()) != undefined ){
       
    99       msg += showObject( item );
       
   100     }
       
   101   }
       
   102   catch(e)
       
   103   {
       
   104     alert('&lt;showIterableList&gt; ' + e);
       
   105   }
       
   106   return msg;
       
   107 }
       
   108 
       
   109 // Build the message by reading a JS object in a recursive manner
       
   110 function showObject( obj )
       
   111 {
       
   112   var txt = "";
       
   113   try { 
       
   114     if ( typeof obj != 'object' )
       
   115       return "" + obj + '&lt;BR/&gt;';
       
   116     else {
       
   117       for(var key in obj) {
       
   118         txt +=  key + ":";
       
   119         txt += showObject( obj[key] );
       
   120         txt += '&lt;BR/&gt;';
       
   121       }
       
   122       txt += '&lt;BR/&gt;';
       
   123     }
       
   124   }
       
   125   catch (e)
       
   126   {
       
   127     alert("showObject: " + e);
       
   128   }
       
   129   return txt;
       
   130 }
       
   131 
       
   132 // Show the image to indicate the test result
       
   133 function showIMG(imgId, isOK) 
       
   134 {
       
   135   if(imgId == null || imgId == undefined)
       
   136     return;
       
   137 
       
   138   if(isOK == "yes")
       
   139     document.getElementById(imgId).src = "pic/yes.png";
       
   140   else if(isOK == "no")
       
   141     document.getElementById(imgId).src = "pic/no.png";
       
   142   else
       
   143     document.getElementById(imgId).src = "pic/blank.png";
       
   144 }
       
   145 
       
   146 // Show elements in object by using 'alert'
       
   147 function testObject(obj)
       
   148 {
       
   149   var msg = "";
       
   150   for(var key in obj) {
       
   151     msg = msg + ":" + key + "=" + obj[key];
       
   152   }
       
   153   alert(msg);
       
   154 }
       
   155 
       
   156 // Test whether the input is numeric
       
   157 function IsNumeric(sText)
       
   158 {
       
   159   var ValidChars = "0123456789.";
       
   160   var IsNumber=true;
       
   161   var Char;
       
   162 
       
   163   for (i = 0; i &lt; sText.length &amp;&amp; IsNumber == true; i++) 
       
   164   {
       
   165     Char = sText.charAt(i); 
       
   166     if (ValidChars.indexOf(Char) == -1) 
       
   167     {
       
   168       IsNumber = false;
       
   169     }
       
   170   }
       
   171   return IsNumber; 
       
   172 }
       
   173 </pre>
       
   174 </div>
       
   175 <div><h3>location-sample.js</h3>
       
   176 <pre class="codeblock" id="GUID-43A0ABBF-478C-4700-A339-DA8A5248EC59">// location-sample.js
       
   177 // 
       
   178 // In this sample Location will be listed, traced, and calculated. 
       
   179 // Also, Also, async operation will be canceled.
       
   180 //
       
   181 
       
   182 //SAPI Error Codes
       
   183 // 0  - Success
       
   184 // 1000 - InvalidServiceArgument
       
   185 // 1001 - UnknownArgumentName
       
   186 // 1002 - BadArgumentType
       
   187 // 1003 - MissingArgument
       
   188 // 1004 - ServiceNotSupported
       
   189 // 1005 - ServiceInUse
       
   190 // 1006 - ServiceNotReady
       
   191 // 1007 - NoMemory
       
   192 // 1008 - HardwareNotAvailable
       
   193 // 1009 - ServerBusy
       
   194 // 1010 - EntryExists
       
   195 // 1011 - AccessDenied
       
   196 // 1012 - NotFound
       
   197 // 1013 - UnknownFormat
       
   198 // 1014 - GeneralError
       
   199 // 1015 - CancelSuccess
       
   200 // 1016 - ServiceTimedOut
       
   201 // 1017 - PathNotFound
       
   202 
       
   203 // Declare the service object 
       
   204 var so;
       
   205 
       
   206 // imgid for callback1 function
       
   207 var imgid_callback1;
       
   208 
       
   209 // imgid for callback2 function
       
   210 var imgid_callback2;
       
   211 
       
   212 // id of the div used to display information
       
   213 const DIV_ID = 'locationsample';
       
   214 
       
   215 // Called from onload()
       
   216 function setup()
       
   217 {
       
   218   try {
       
   219     so = device.getServiceObject("Service.Location", "ILocation");
       
   220     console.info("setup: so: %s", so);
       
   221   }
       
   222   catch (e) {
       
   223     alert('&lt;setup&gt; ' +e);
       
   224   }
       
   225 }
       
   226 
       
   227 // Get Location
       
   228 function getLocation(imgId) {
       
   229 
       
   230   // This specifies update option used while retrieving location estimation. 
       
   231   var updateoptions = new Object();
       
   232   // Setting PartialUpdates to 'FALSE' ensures that user get atleast 
       
   233   // BasicLocationInformation (Longitude, Lattitude, and Altitude.)
       
   234   updateoptions.PartialUpdates = false;
       
   235 
       
   236   var criteria = new Object();
       
   237   criteria.LocationInformationClass = "GenericLocationInfo";
       
   238   criteria.Updateoptions = updateoptions;
       
   239 
       
   240   try {
       
   241     var result = so.ILocation.GetLocation(criteria);
       
   242     if(!checkError("ILocation::getLocation",result,DIV_ID,imgId)) {
       
   243       document.getElementById(DIV_ID).innerHTML = showObject(result.ReturnValue);
       
   244     }
       
   245   }
       
   246   catch (e) {
       
   247     showIMG(imgId,"no");
       
   248     alert ("getLocation: " + e);
       
   249   }
       
   250 }
       
   251 
       
   252 // Get Location Async
       
   253 function getLocationAsync(imgId) {
       
   254 
       
   255   // This specifies update option used while retrieving location estimation. 
       
   256   var updateoptions = new Object();
       
   257   // Setting PartialUpdates to 'FALSE' ensures that user get atleast 
       
   258   // BasicLocationInformation (Longitude, Lattitude, and Altitude.)
       
   259   updateoptions.PartialUpdates = false;
       
   260 
       
   261   var criteria = new Object();
       
   262   criteria.LocationInformationClass = "GenericLocationInfo";
       
   263   criteria.Updateoptions = updateoptions;
       
   264 
       
   265   // Set image id for callback1 function
       
   266   imgid_callback1 = imgId;
       
   267 
       
   268   try {
       
   269     var result = so.ILocation.GetLocation(criteria, callback1);
       
   270     if(!checkError("ILocation::getLocationAsync",result,DIV_ID,imgId)) {
       
   271       showIMG(imgId,"");
       
   272     }
       
   273   }
       
   274   catch (e) {
       
   275     showIMG(imgId,"no");
       
   276     alert ("getLocationAsync: " + e);
       
   277   }
       
   278 }
       
   279 
       
   280 // Trace Location
       
   281 function traceLocation(imgId) {
       
   282 
       
   283   // This specifies update option used while retrieving location estimation. 
       
   284   var updateoptions = new Object();
       
   285   // Setting PartialUpdates to 'FALSE' ensures that user get atleast 
       
   286   // BasicLocationInformation (Longitude, Lattitude, and Altitude.)
       
   287   updateoptions.PartialUpdates = false;
       
   288 
       
   289   var criteria = new Object();
       
   290   criteria.LocationInformationClass = "GenericLocationInfo";
       
   291   criteria.Updateoptions = updateoptions;
       
   292 
       
   293   // Set image id for callback2 function
       
   294   imgid_callback2 = imgId;
       
   295 
       
   296   try {
       
   297     var result = so.ILocation.Trace(criteria, callback2);
       
   298     if(!checkError("ILocation::traceLocation",result,DIV_ID,imgId)) {
       
   299       showIMG(imgId,"");
       
   300     }
       
   301   }
       
   302   catch (e) {
       
   303     showIMG(imgId,"no");
       
   304     alert ("traceLocation: " + e);
       
   305   }
       
   306 }
       
   307 
       
   308 // Cancel Notification
       
   309 function cancelNotification(imgId) {
       
   310 
       
   311   // "TraceCancel"
       
   312   // "GetLocCancel"
       
   313   var msg = "Please enter the type ['GetLocCancel' or 'TraceCancel']";
       
   314   var cancelRequestType = prompt(msg, "TraceCancel");
       
   315 
       
   316   if(cancelRequestType == "" || cancelRequestType == null)
       
   317     return;
       
   318 
       
   319   var criteria = new Object();
       
   320   criteria.CancelRequestType = cancelRequestType;
       
   321 
       
   322   try {
       
   323     var result = so.ILocation.CancelNotification(criteria);
       
   324     checkError("ILocation::cancelNotification",result,DIV_ID,imgId);   
       
   325   }
       
   326   catch (e) {
       
   327     showIMG(imgId,"no");
       
   328     alert ("cancelNotification: " + e);
       
   329   }
       
   330 }
       
   331 
       
   332 // Calculate Location
       
   333 function calculateLocation(imgId) {
       
   334 
       
   335   // "FindDistance"
       
   336   // "FindBearingTo"
       
   337   // "MoveCoordinates"
       
   338   var msg = "enter ['FindDistance' or 'FindBearingTo' or 'MoveCoordinates']";
       
   339   var mathRequest = prompt(msg, "FindDistance");
       
   340   
       
   341   if(mathRequest == "" || mathRequest == null)
       
   342     return;
       
   343 
       
   344   var distanceSrc = new Object();
       
   345   distanceSrc.Latitude = 40.69;
       
   346   distanceSrc.Longitude = 74.04; 
       
   347   distanceSrc.Altitude = 59;
       
   348 
       
   349   var distanceDest = new Object();
       
   350   distanceDest.Latitude = 42.48;
       
   351   distanceDest.Longitude = 71.19;
       
   352   distanceDest.Altitude = 2;
       
   353 
       
   354   var criteria = new Object();
       
   355   criteria.MathRequest = mathRequest;
       
   356   criteria.DistanceParamSource = distanceSrc;
       
   357   criteria.DistanceParamDestination = distanceDest;
       
   358   // The two parameters below are applicable only for Move coordinates (not for Find Distance and Find Bearing)
       
   359   criteria.MoveByThisDistance = 10.0;
       
   360   criteria.MoveByThisBearing = 10.0;
       
   361 
       
   362   try {
       
   363     var result = so.ILocation.Calculate(criteria);
       
   364     if(!checkError("ILocation::Calculate",result,DIV_ID,imgId)) {
       
   365       document.getElementById(DIV_ID).innerHTML = showObject(result.ReturnValue);
       
   366     }
       
   367   }
       
   368   catch (e) {
       
   369     showIMG(imgId,"no");
       
   370     alert ("calculateLocation: " + e);
       
   371   }
       
   372 }
       
   373 
       
   374 // This is the asynchronous callback handler 
       
   375 function callback1(transId, eventCode, result)
       
   376 {
       
   377   console.info("getLocationAsync: transId: %d  eventCode: %d result.ErrorCode: %d", transId, eventCode, result.ErrorCode);
       
   378   if(!checkError("ILocation::getLocationAsync",result,DIV_ID,imgid_callback1)) {
       
   379     document.getElementById(DIV_ID).innerHTML = showObject(result.ReturnValue);
       
   380   }
       
   381 }
       
   382 
       
   383 // This is the asynchronous callback handler 
       
   384 function callback2(transId, eventCode, result)
       
   385 {
       
   386   console.info("traceLocation: transId: %d eventCode: %d result.ErrorCode: %d", transId, eventCode, result.ErrorCode);
       
   387   if(!checkError("ILocation::traceLocation",result,DIV_ID,imgid_callback2)) {
       
   388     document.getElementById(DIV_ID).innerHTML = showObject(result.ReturnValue);
       
   389   }
       
   390 }
       
   391 </pre>
       
   392 </div>
       
   393 </div></div></div><div class="footer"><hr/><div class="copy">© Nokia 2009.</div></div></body></html>