/ Published in: PHP
A PHP function for calculating the distance between two UK postcodes
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
<?php function getDistance($postcode1, $postcode2) { $coordinates1 = file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?address=' . urlencode($postcode1) . '&sensor=true'); $coordinates2 = file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?address=' . urlencode($postcode2) . '&sensor=true'); $earth_radius = 6371; $dLat = deg2rad($coordinates2->results[0]->geometry->location->lat - $coordinates1->results[0]->geometry->location->lat); $dLon = deg2rad($coordinates2->results[0]->geometry->location->lng - $coordinates1->results[0]->geometry->location->lng); $d = $earth_radius * $c; $d = $d * 0.621; return $d; } echo getDistance('W1T 1AL', 'WD6 1JG'); ?>