HOME | TOOLS | API | MAP

PlaceEngine API Programming Samples

Here we introduce a collection of programming samples that use the PlaceEngine API.

Sample 1: Hybrid Position Estimation using JavaScript

This sample estimates the current position (latitude/longitude) using the Hybrid Estimation feature (only available when using the Windows version of PlaceEngine Client). Please click here to run this sample in a separate page.

<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>PlaceEngine Logger</title>
<script type="text/javascript">
var timer=null;
var appk="......"; // specify the application key here
var lastElement=null;

function startLogging()
{
  timer = setTimeout("getPosition()",0);
}

function stopLogging()
{
  if( timer != null ){
    clearTimeout(timer);
    timer=null;
  }
}

function clearLog()
{
  document.getElementById("output").innerHTML = "";
}

function getPosition()
{
  if( timer==null )
    return;

  var script = document.createElement('script');
  script.type = "text/javascript";
  script.charset = "UTF-8";
  script.src = "http://localhost:5448/getlocjs?m=g,l,p&usecache=0&fmt=jsonp&appk=" + appk
             + "&t=" + parseInt(((new Date()).getTime()/1000));
  if( lastElement!=null ){
    document.body.removeChild(lastElement);
    lastElement = null;
  }
  lastElement = document.body.appendChild(script);
}

function recvLoc2(results)
{
  if( timer==null )
    return;

  if(!(results instanceof Array)) {
    results = [results];
  }

  for(var i=0; i<results.length; i++) {
    var data = results[i];

    var t = new Date();
    t.setTime(data.t*1000);

    var method="";
    switch(data.m){
      case "p": method="PlaceEngine server"; break;
      case "l": method="LocalDB"; break;
      case "g": method="GPS"; break;
    }

    var str = "time=" + t.toLocaleString()
            + ", result=" + data.r
            + "," + (data.r >= 0 ? " lat=" + data.y : "")
            + "," + (data.r >= 0 ? " lon=" + data.x : "")
            + ", method=" + method
            ;
    if( data.info != null && data.info.addr != null )
      str += ", addr=" + data.info.addr;
    str += "<br>";
    document.getElementById("output").innerHTML += str;
  }

  if( timer!=null )
    timer = setTimeout("getPosition()",10000);
}

</script>
</head>
<body>
<input type="button" value="Start Logging" onClick="javascript:startLogging()">
<input type="button" value="Stop Logging" onClick="javascript:stopLogging()">
<input type="button" value="Clear Log" onClick="javascript:clearLog()">
<hr>
<span id="output"></span>
</body>
</html>

©2007-2011 Koozyt, Inc. | Japanese / English