Reverse geocoding to find an address

Convert geographic coordinates to their nearest address.


This reverse geocoding method finds the nearest UK address and postcode from the geolocation of latitude and longitude co-ordinates given.

This can be used to find an address from a map location of co-ordinates, or verify that an existing UK address in your records is correct.

API method to use: reverse_geo_code

How it works

When a longitude and latitude pair is supplied, the method will return the nearest UK address and postcode.

Please see the Documentation section for more information including a list of mandatory and optional parameters, and returned values

API method to use: reverse_geo_code
|
Credits per use: 2

Converts a position (latitude, longitude) into the nearest UK address and postcode.

Used in conjunction with the geo_code_telephone method, you can find the nearest addressable location of your mobile staff at any time. Our address data is updated monthly meaning you will always be receiving the most accurate information available.

Mandatory Parameters


Name Description
api_key or javascript_key Use your API key if invoking server-side. If invoking from the browser via JavaScript, generate a JavaScript key using the javascript_key method.
latitude

WGS 84(GPS) co-ordinates.

Values for the UK are around 49 to 59 degrees.

longitude

WGS 84(GPS) co-ordinates.

Values to cover the UK and Ireland are around -11 to 3 degrees.

Optional Parameters


Name Description
client You can optionally include an identifier for your final client or user. This is logged in your usage log and you will be able to view your usage statistics by client.
output Set to json for a JSON response; the default response is XML. Not applicable to the SOAP API.
callback When using JSON, specify a JSONP wrapper in which the JSON response is to be wrapped.

Returned Values


The XML response is contained within a <reverse_geo_code_res> element.

Name Description
status Returns ok if the operation has succeeded, or error if an error has occurred; Returns the error_code for error details.
error_code Returns the error code when the status is error. See below for error codes.
t2a_version_number The current API version number.
credit_used The number of credits used in order to execute the request.
mode Returns normal or test when executed in the free test mode.
address An address record, the closest to the specified co- ordinates.

Error Codes


Name

Description

missing_lat

Mandatory parameter

missing_lon

Mandatory parameter

invalid_latitude

The latitude (lat) value is invalid (outside of the UK area).

See the common error codes.

Free Test Mode


When operating in the free test mode errors are returned if mandatory parameters are missing, or it returns a dummy data response, in an identical format to a real response.

Note that the mode is returned as test when the method is operating in the free test mode.

Example XML Response



<?xml version="1.0"?>
<reverse_geo_code_res>
  <t2a_version_number>1.0.0.8</t2a_version_number>
  <status>ok</status>
  <address>
	<line_1>4 Imagination Gardens</line_1>
	<line_2>Magic Street</line_2>
	<line_3 />
	<place>Heslingdown</place>
	<town>York</york>
	<addr_single_line>4 Imagination Gardens, Magic Street, Heslingdown, York, YO10 5NP</addr_single_line>
	<postcode>YO10 5NP</postcode>
  </address>
</reverse_geo_code_res>

Enter a latitude and longitude pair to find the nearest address.

There are two ways to authenticate your application with the T2A API. The two implementation examples on this page cover each type of authentication

We recommend using an API key for internal applications and the javascript key for public facing applications where you would want to protect your API key.

Please note: The examples below run on a sandbox environment which return sample data. To quality check the data we provide, up to 3 free searches are available in the demo tab with results showing live [REDACTED] data.

API key


<div class="reverse-geo-code-example">
  <form class="reverse-geo-code-form">

    <div class="form-group">
          <label for="surname">Longitude</label>
          <input type="text" class="form-control" id="latitude" placeholder="e.g. 53.9508364147783">
      </div>

    <div class="form-group">
          <label for="surname">Longitude</label>
          <input type="text" class="form-control" id="longitude" placeholder="e.g. -1.04555083530746">
      </div>

    <button type="submit" class="btn example-submit">Submit</button>
  </form>
</div>

<div class="results">
  <div id="results-output"></div>
  <a class="results-return" href="#">Back to search</a>
</div>
                
.reverse-geo-code-example, .results {
  margin: 20px auto;
  width: 400px;
}

form.reverse-geo-code-form, .results {
    background-color: #F6F6F6;
    border: 1px solid #CBCBCB;
    padding: 15px;
}

.example-submit {
    background-color: #F0614C;
    border-radius: 2px;
    font-size: 11px;
    font-weight: 400;
    color: #fff;
    text-transform: uppercase;
    letter-spacing: 0.193em;
    width: 138px;
    height: 41px;
    margin-top: 10px;
}

.results {
  display: none;
}

.results-return {
    font-weight: 600;
    color: #F0614C;
}

#results-output span {
    font-weight: bold;
    text-transform: capitalize;
}

#results-output hr {
    border-top: 1px solid #000;
}

.error {
  font-size: 11px;
  color: #f00;
}
                
$(document).ready(function(){ 	
    $(".reverse-geo-code-form").submit(function(e) {
        e.preventDefault();	

        $('.error', $(this)).remove();

        var latitude = $("#latitude");        if(!latitude.val()) {
                errorBefore("Please enter a longitude.", latitude);
            }

        var longitude = $("#longitude");        if(!longitude.val()) {
                errorBefore("Please enter a longitude.", longitude);
            }

        if($('.error', $(this)).length) {	
            $('.error', $(this)).first().next('input').focus();
        } else {
    
            $.ajax({
                url: 'https://api.t2a.io/rest/rest.aspx',
                dataType: 'json',
                data: {
                    'method'   : "reverse_geo_code",
                    'api_key'  : 'sandbox',
                    'latitude' : latitude.val(),
                    'longitude' : longitude.val(),
                    'output'   : 'json'
                    },
                success: function(result){
                    if(result.status == "error") {
                        $('#results-output').append('<p class="output"><strong>Error:</strong>' + result.error_code +'</p>');
                    } else {
                        var i = 0;
                        if(typeof result.address_list !== "undefined") {
                            result.address_list.forEach(function(address_list_item){
                                if(i>0){
                                    $('#results-output').append('<hr>');
                                }
                                var exceptionsArray = ['META', 'created_details', 'years_list', 'address_key', 'organisation_key', 'dob_details'];
                                    
                                for (var key in address_list_item) {
                                    if(!key.includes('id') && !exceptionsArray.includes(key) ) {
                                        var keyName = key.replace('_', ' ');
                                        $('#results-output').append('<p class="output"><span>' + keyName + ':</span> ' + address_list_item[key] + '</p>');
                                    }
                                }
                                i++;
                            });
                        }
                    }
                    $('.reverse-geo-code-example').hide();
                    $('.results').show(); 
               }
            });
        }
    });
    
    $('.results-return').on('click', function(e){
        e.preventDefault();
        $('#results-output').empty();
        
        $('.reverse-geo-code-example').show();
        $('.results').hide();
    });
});

function errorBefore(msg, insertBefore) {
    $('<p class="error">' + msg + '</p>').insertBefore(insertBefore);
}

Javascript key


<div class="reverse-geo-code-example">
  <form class="reverse-geo-code-form">

    <div class="form-group">
          <label for="surname">Longitude</label>
          <input type="text" class="form-control" id="latitude" placeholder="e.g. 53.9508364147783">
      </div>

    <div class="form-group">
          <label for="surname">Longitude</label>
          <input type="text" class="form-control" id="longitude" placeholder="e.g. -1.04555083530746">
      </div>

    <button type="submit" class="btn example-submit">Submit</button>
  </form>
</div>

<div class="results">
  <div id="results-output"></div>
  <a class="results-return" href="#">Back to search</a>
</div>
                
.reverse-geo-code-example, .results {
  margin: 20px auto;
  width: 400px;
}

form.reverse-geo-code-form, .results {
    background-color: #F6F6F6;
    border: 1px solid #CBCBCB;
    padding: 15px;
}

.example-submit {
    background-color: #F0614C;
    border-radius: 2px;
    font-size: 11px;
    font-weight: 400;
    color: #fff;
    text-transform: uppercase;
    letter-spacing: 0.193em;
    width: 138px;
    height: 41px;
    margin-top: 10px;
}

.results {
  display: none;
}

.results-return {
    font-weight: 600;
    color: #F0614C;
}

#results-output span {
    font-weight: bold;
    text-transform: capitalize;
}

#results-output hr {
    border-top: 1px solid #000;
}

.error {
  font-size: 11px;
  color: #f00;
}
                
$(document).ready(function(){ 	
    $(".reverse-geo-code-form").submit(function(e) {
        e.preventDefault();	

        $('.error', $(this)).remove();

        var latitude = $("#latitude");        if(!latitude.val()) {
                errorBefore("Please enter a longitude.", latitude);
            }

        var longitude = $("#longitude");        if(!longitude.val()) {
                errorBefore("Please enter a longitude.", longitude);
            }

        if($('.error', $(this)).length) {	
            $('.error', $(this)).first().next('input').focus();
        } else {
            $.ajax({
                url: 'https://t2a.io/ajax/getExampleJSKey',
                dataType: 'json',
                success: function (result) {       
                        
                    if (result.status)
                    {       
                        $.ajax({
                            url: 'https://api.t2a.io/rest/rest.aspx',
                            dataType: 'json',
                            data: {
                                'method'   : "reverse_geo_code",
                                'javascript_key'  : result.javascript_key,
                                'domain' : "t2a.io", 
                                'latitude' : latitude.val(),
                                'longitude' : longitude.val(),
                                'output'   : 'json'
                            },
                            success: function(result){
                        if(result.status == "error") {
                            $('#results-output').append('<p class="output"><strong>Error:</strong>' + result.error_code +'</p>');
                        } else {
                            var i = 0;
                            if(typeof result.address_list !== "undefined") {
                                result.address_list.forEach(function(address_list_item){
                                    if(i>0){
                                        $('#results-output').append('<hr>');
                                    }
                                    
                                    var exceptionsArray = ['META', 'created_details', 'years_list', 'address_key', 'organisation_key', 'dob_details'];
                                    
                                    for (var key in address_list_item) {
                                        if(!key.includes('id') && !exceptionsArray.includes(key) ) {
                                            var keyName = key.replace('_', ' ');
                                            $('#results-output').append('<p class="output"><span>' + keyName + ':</span> ' + address_list_item[key] + '</p>');
                                        }
                                    }
                                    i++;
                                });
                            }
                       }
                                $('.reverse-geo-code-example').hide();
                                $('.results').show(); 
                            }
                        });
                    }
                }
            });
        }
    });
    
    $('.results-return').on('click', function(e){
        e.preventDefault();
        $('#results-output').empty();
        
        $('.reverse-geo-code-example').show();
        $('.results').hide();
    });
});

function errorBefore(msg, insertBefore) {
    $('<p class="error">' + msg + '</p>').insertBefore(insertBefore);
}

This example first needs to call to a file on your server, which will provide the user with an API key from your javascript key.

In our example above we have used a PHP file located at ajax/getExampleJSKey which looks like the example below

    <?php

        $url = 'https://api.t2a.io/rest/rest.aspx'
            . "?method=javascript_key"
            . "&api_key=sandbox" .
            . "&domain=" . $_SERVER['HTTP_HOST'];
            . '&ip_address=' . get_user_ip()
            . "&lifetime_minutes=10";

        $result = simplexml_load_file($url);

        if ($result->javascript_key) {
            echo (string)$result->javascript_key;
        }

Next Steps...


We will give you a unique API key to allow you to test the method using credits on your T2A account. The Demo tab will allow you to test the parameters required and view the result.

If you do not have an account you will first need to sign up

 

Get in touch

 

 

Other related API methods:

Geo-code an address Geo-code a single address