Skip to content
Switch branches/tags
Go to file
Cannot retrieve contributors at this time
<!DOCTYPE html>
<html lang="en">
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>UK geocoding with jQuery</title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
body { padding: 5%; }
#postcode { margin-bottom: 20px; }
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src=""></script>
<script src=""></script>
<div class="row">
<div class="col-xs-12">
<h1>UK geocoding with jQuery</h1>
<p>Simple demo which passes postcode to the API on form submit and returns latitude and longitude.</p>
<p><a href="">Full geocoding API documentation</a></p>
<form id="example_form">
<div class="form-group">
<label for="postcode">Postcode</label>
<input type="postcode" class="form-control" id="postcode" placeholder="Postcode">
<button type="submit" class="btn btn-default" id="geocode">Geocode</button>
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src=""></script>
// Replace with your API key, test key will always return latitude and longitude for "NR14 7PZ"
var api_key = "PCW45-12345-12345-1234X";
$(document).ready(function() {
$("#example_form").on("submit", function(e) {
function geocode_postcode(element) {
// Grab the input text and trim any whitespace
var postcode = $(element).val().trim();
if (postcode != "") {
// Remove any previous geocoding results
// Create a template bootstrap alert div
var response_html = $("<div></div>", {
class: "well",
id: "geocode_result"
// Create a loading message
var loading_html = $("<div></div>", {
id: "geocode_loading",
text: "Geocoding postcode..."
// Create the URL to API including API key and encoded postcode
var geocode_url = "" + api_key + "/position/UK/" + encodeURIComponent(postcode);
// Call the API
url: geocode_url
}).done(function(data) {
if(data.length > 0) {
response_html.html("Latitude: " + data[0].latitude + "<br>Longitude: " + data[0].longitude);
} else {
response_html.text("Postcode: \"" + postcode + "\" Not found.");
}).fail(function(error) {
// Triggered if API cannot be reached or does not return 200 HTTP code
// More info -
} else {
// Could show an "postcode is required" message here