This document should help you get started with the new LocationWEB Javascript API.
<!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>LocationWEB Javascript API</title>
<script type="text/javascript" src="http://locationweb.mobi/javascript/locationweb.js"></script>
<script type="text/javascript">
function load()
{
var geo = LocationWEB();
if (geo) {
geo.onSuccess = function(location) {alert(location.latitude+', '+location.longitude)}
geo.onFailure = function(error) {alert(error)}
geo.requestLocation(true,geo.NO_STREET_ADDRESS_LOOKUP);
}
}
</script>
</head>
<body onLoad="load();">
<h1>Simple LocationWEB Javascript API Example</h1>
</body>
</html>
Here you see a simple example of how to implement the API in only 6 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.
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 LocationWEB whether or not you want the users 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 LocationWEB object so that it makes it easier to remember. There are 3 options:
- geo.NO_STREET_ADDRESS_LOOKUP - 0
- geo.LIMITED_STREET_ADDRESS_LOOKUP - 1
- city
- region
- region_code
- postal_code
- country
- country_code
- geo.FULL_STREET_ADDRESS_LOOKUP - 2
- house_number
- street
- city
- region
- region_code
- postal_code
- country
- country_code
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 folloing attributes based on the paramter 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
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