Getting Started with the Javascript API
This document should help you get started with the new Loki Javascript API. First you will need to go to the Loki site and create an Application Key for your site. Remember this key, you will need it when implementing the Javascript API.
Get started in 7 lines of code
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Loki Javascript API</title>
<script type="text/javascript" src="http://loki.com/javascript/loki.js"></script>
<script type="text/javascript">
//<![CDATA[
function load()
{
var loki = LokiAPI();
if (loki) {
loki.onSuccess = function(location) {alert(location.latitude+', '+location.longitude)}
loki.onFailure = function(error) {alert(error)}
loki.setKey('[your_key_here]');
loki.requestLocation(true,loki.NO_STREET_ADDRESS_LOOKUP);
}
}
</script>
</head>
<body onLoad="load();">
<h1>Simple Loki Javascript API Example</h1>
</body>
</html>
Here you see a simple example of how to implement the API in only 7 simple lines. Now you will probably want to do some more intelligent error handling and do something a little more interesting with the user's location, but as you can see its easy to integrate the new API.
Lets go through the steps...
- new Loki() or new ActiveXObject("Loki.LocationFinder.1") Create a new instance of the Loki object. In Firefox it's a native call, in Internet Explorer you need to create a new ActiveX object.
- loki.onSuccess = function(location){} Setup your onSuccess handler -- it gets passed a 'location' object
- loki.onFailure = function(error){} Setup your onFailure handler -- it gets passed an error code
- loki.setKey() Set your Application Key. If you don't already have one, you can get one by going to http://loki.com/developers/register/
- loki.requestLocation(true,loki.NO_STREET_ADDRESS_LOOKUP); Finally, request the location from the toolbar
Thats it! If you want to learn more about each step, keep reading. If you have any other questions please email us at support@loki.com and we will be happy to help
Reference
Loki.requestLocation(latlon_lookup, address_lookup )
This method initiates the scan and the actual location request. It has two parameters that define what information you want to request from the user.
- latlon_lookup bool - this parameter tells Loki whether or not you want the user's latitude and longitude as part of the location object.
- address_lookup int - This parameter identifies if you want the full reverse geocode or a subset of that returned as part of the location object. We have set some constants in the Loki object so that it makes it easier to remember. There are 3 options:
- loki.NO_STREET_ADDRESS_LOOKUP - 0
- loki.LIMITED_STREET_ADDRESS_LOOKUP - 1
- city
- region
- region_code
- postal_code
- country
- country_code
- loki.FULL_STREET_ADDRESS_LOOKUP - 2
- house_number
- street
- city
- region
- region_code
- postal_code
- country
- country_code
Loki.setKey( string key )
setKey requires one parameter, key, which is the Application Key you were assigned when you registered with loki.com. If you have not registered, you can do so by visiting http://loki.com/developers/register/. If the key is invalid, the onFailure callback will be executed with error code 5.
Loki.onSuccess( object location )
If the call to the server executes successfully, this callback function will be executed and passed a location object. The location object could have the following attributes based on the parameter used during the requestLocation() call:
latitude A floating number. Example: 42.350827 longitude A floating number. Example: -71.049465 house_number The house number of the street address. Example: 332 in "332 Congress St" street The name of the street. Example: Congress St in "332 Congress St" city The name of the city: Example: Boston region Internationalized. In most countries this maps to a State name, Province name or the equivalent. Example: Massachusetts or Ontario region_code Internationalized. In most countries this maps to a State code, Province code or the equivalent. Example: MA or ON postal_code In countries that support postal codes this field will be populated. Example: 02210 or A9A A9A country The name of the country. Example: United States country_code The character code representing the country: Example: US
Loki.onFailure( int error )
If the request to the server fails this callback function will be executed and passed an error code. A request could fail for a number of reasons, either client-side or server side. The error code will denote what type of error occurred. The following error codes are possible:
- 1 - Scanner not found
- 2 - WiFi not available
- 3 - No WiFi in range
- 4 - Unauthorized
- 5 - Invalid Application Key
- 6 - Location cannot be determined
- 7 - Proxy unauthorized
Support
If you have any questions, comments or bugs to report you can email us at support@loki.com or you can go to http://loki.com/support.
