Link to home
Start Free TrialLog in
Avatar of shopjd
shopjd

asked on

enter zip code, calculate distance from my zip code

Is there a way to calculate distance from my zip code

by allowing user to enter their zip code in a text box
Avatar of pmessana
pmessana

You will have to write something.  Rather than reinventing the wheel, take a look at this.

http://www.micahcarrick.com/04-19-2005/php-zip-code-range-and-distance-calculation.html

and

http://www.idealog.us/2006/07/zip_code_distan.html
You need a zipcode database with longitude and latitude coordinates.  Then you have to calculate the distance between the two points.  

This link below should help you it has a link to download a database and it has some sample code.  Let me know if you still need help.

http://www.idealog.us/2006/07/zip_code_distan.html
ASKER CERTIFIED SOLUTION
Avatar of shobinsun
shobinsun
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of shopjd

ASKER

-shobinsun

I added
x->query('90035');

comparing it with


$y->query('90210');

the output is
5190.44 kms

how can I change the code so a user can input their zip code
and outputs distance in miles
Hello,

Use this method:

$first = $_GET['distance1'];

$second = $_GET['distance2'];

x->query($first);

$y->query($second);

the  $_GET['distance1']  and $_GET['distance2']  are the values entered by the user in a form.

Regards.
Avatar of shopjd

ASKER

which file do I edit
Hello,

You shoud edit the file: ext1.php
Hello,

Here is the full code:

post.html(for user input):

<html>
<form action="ex1.php" method="post">
First code<input type="text" name="distance1"/>
Second code<input type="text" name="distance2"/>
<input type="Submit" name="submit" value="Send"/>
</form>
</html>


ex1.php:

<?php

include 'class.geodata.inc.php';



// Compare the distance between two different zip/postal codes

$x = new EyeGeoData();


$first = $_POST['distance1'];

$second = $_POST['distance2'];

$x->query($first);



$y = new EyeGeoData();

$y->query($second);



echo $x->calcDistance($y, EyeGeoData::KMS, 2) . ' kms'; // prints "6923.05 kms"
?>


regards.
Avatar of shopjd

ASKER

line 291  of class.geodata.inc.php:
which is  
trigger_error('Result empty; query either failed or has not been run', E_USER_ERROR);

makes an error:
Error: E_USER_ERROR
Result empty; query either failed or has not been run
at class.geodata.inc.php line 291
Hello,

The error is because of the invalid entry of code or you have entered only one code in the user field.

Which code you have entered ?

Regards.
Avatar of shopjd

ASKER

10021 10001  generates 6114.18 kms
10021 90210  generates 7050.58 kms

is there anyway to convert kms to miles
Hello,

You can convert kms to miles in the code as:

$dist=$x->calcDistance($y, EyeGeoData::KMS, 2);
$dist=$dist/1.6093;
echo $x->calcDistance($y, EyeGeoData::KMS, 2) . ' kms';
echo $dist . ' miles';

regards.
Avatar of shopjd

ASKER

thank you for the kms to miles conversion

but

10021 new york 10001 new york  generates 6114.18 kms
10021 new york 90210 los angeles generates 7050.58 kms

the distance is wrong
Avatar of shopjd

ASKER

hank you for the kms to miles conversion

but

10021 new york 10001 new york  generates 6114.18 kms
10021 new york 90210 los angeles generates 7050.58 kms

the distance is wrong
Hi,

Please find the script from the following link:

http://rapidshare.com/files/222096046/ZIPCODE.zip.html

I have uploaded a very good script for you.

hope this will help you.

Sorry for the delay.

Thanks and regards.
Hi,

To calculate the distance between two UK postcodes, please go through the link:

http://www.sloomedia.com/php_postcode_script.php

Regards.
Hello,

here is an another script for you.

Try with it.

http://rapidshare.com/files/222345520/zipcode.zip.html

Thanks and Regards