Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
129 lines (90 sloc) 5.05 KB
<!DOCTYPE html>
<html lang="en">
<head>
<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="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<style>
body { padding: 5%; }
#postcode { margin-bottom: 20px; }
</style>
<!-- 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="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<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="https://developers.alliescomputing.com/postcoder-web-api/geocoding/position">Full geocoding API documentation</a></p>
<br><hr><br>
<form id="example_form">
<div class="form-group">
<label for="postcode">Postcode</label>
<input type="postcode" class="form-control" id="postcode" placeholder="Postcode">
</div>
<button type="submit" class="btn btn-default" id="geocode">Geocode</button>
</form>
</div>
</div>
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<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) {
e.preventDefault();
geocode_postcode("#postcode");
})
});
function geocode_postcode(element) {
// Grab the input text and trim any whitespace
var postcode = $(element).val().trim();
if (postcode != "") {
// Remove any previous geocoding results
$("#geocode_result").remove();
// 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..."
});
$(element).after(loading_html);
// Create the URL to API including API key and encoded postcode
var geocode_url = "https://ws.postcoder.com/pcw/" + api_key + "/position/UK/" + encodeURIComponent(postcode);
// Call the API
$.ajax({
url: geocode_url
}).done(function(data) {
$("#geocode_loading").remove();
if(data.length > 0) {
response_html.html("Latitude: " + data[0].latitude + "<br>Longitude: " + data[0].longitude);
$(element).after(response_html);
} else {
response_html.text("Postcode: \"" + postcode + "\" Not found.");
$(element).after(response_html);
}
}).fail(function(error) {
$("#geocode_loading").remove();
// Triggered if API cannot be reached or does not return 200 HTTP code
// More info - https://developers.alliescomputing.com/postcoder-web-api/error-handling
});
} else {
// Could show an "postcode is required" message here
}
}
</script>
</body>
</html>
You can’t perform that action at this time.