Miles calculator

Hi is there any good scripts out there that can calculate miles based on 2 post codes?

e.g. i have a db where users can add there post code and i want to add a form where users can search in miles based on their post code?

e.g. search for other users based on how many miles away from their post code to other users postcodes, i have a drop down box where they can select how many miles away (5, 10, 25, 25, 35, 50, 100, 200+).

I’ve heard google API has something but not sure how to implement it…

Hi i’m trying to implement a miles from calculator on my site where the user would have their post code stored in a database.

I then want them to be able to search for other users based on how many miles away they are from them… e.g. 5, 10, 15, 25, 35, 50, 100, 200+ miles

Is there any free way to do this??

Perferrable for the world but if not the UK should be fine.

You face a couple of challenges.

When you capture the users postcode, store it and then find the lat,lng of the centre of that postcode.

Google “uk postcode lat long”.

Then when they want to find others near them you apply an algorithm in your sql statement which returns postcodes inside a given circle - something similar as shown in this post.

Googling “find nearest” might turn up others.

Google maps features examples which are usually termed “store finder” which may also help you.

Right i’ve got a database full of longitude and latitude values called ukpostcodes and fields Lat and Long which contains the values

I’v found this site http://zcentric.com/2010/03/11/calculate-distance-in-mysql-with-latitude-and-longitude/

which has SQL code to calculate distance from one a longitude and latitute value then display’s the results

i get this error when i run the SQL code from a php page

Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in C:\wamp\www\ emp\ est.php on line 69

line 69 being where the $query get’s executed

here is my php code


<?php

include('db.php');

$lat = '57.1350';
$lon = '-2.1170';

$query = "SELECT ((ACOS(SIN($lat * PI() / 180) AS distance FROM `ukpostcodes` HAVING distance<=10 ORDER BY distance ASC";
$result = mysql_query($query);

while ($row2 = mysql_fetch_assoc ($result)) {
  echo $row2[distance];
}

Any ideas i’ve spent far too long trying to get this working :frowning:

sorry the query should be this, the above one was where i tried debugging it.


$query = "SELECT ((ACOS(SIN($lat * PI() / 180) * SIN(`lat` * PI() / 180) + COS($lat * PI() / 180) * COS(`lat` * PI() / 180) * COS(($lon – `long`) * PI() / 180)) * 180 / PI()) * 60 * 1.1515) AS distance FROM `ukpostcodes`";

Is there going to be a positive result?
Make sure there is some matching data first, use a lat, lng value from a known postcode (check it out gmaps) and be sure there are some values close to it and check them too.

Isolate the SQL from your PHP
Write that FULL statement out, enter values instead of variable names and enter it directly into your db manually, cut any possible PHP out of the equation totally.

If you get results, then your PHP is at fault, else ask on the place you got it from is all I can say.

The warning you get only indicates the query went wrong. To see why the query isn’t working, change


$result = mysql_query($query);

into


$result = mysql_query($query) or die('mysql error ' . mysql_error() . 'in query: ' . $query);