Forum Moderators: coopster

Message Too Old, No Replies

Distance between cities

         

sfast

3:56 pm on Oct 30, 2007 (gmt 0)

10+ Year Member



I have been asked to do search by a "city" and display the store locations that are in that city.

If there are no stores in that city, it should display the nearest locations in another city.
Can somebody guide me some better free scripts that I can modify to make it work as per company needs?

OutdoorWebcams

4:17 pm on Oct 30, 2007 (gmt 0)

10+ Year Member



Here is the essential part of a script I use myself (derived from an algo used for calculating the angular distance of celestial objects):

$dd = sin($slat)*sin($dlat) + cos($slat)*cos($dlat)*cos($slon-$dlon);
$dist = acos($dd) * $earthradius;

$slon and $slat are the geographical longitude and latitude of the 'source' city, $dlon and $dlat are the same for the 'destination' city.
Southern latitudes and western longitudes are negative, all four values are expressed in radians.

$earthradius is planet earth's radius, (almost) exactly 6371 kilometers.

$dist will contain the distance between the two cities in kilometers (or express earth's radius in miles and get the distance in miles).

sfast

6:37 pm on Oct 30, 2007 (gmt 0)

10+ Year Member



Thanks. I will try this.

I am sure I will have more questions later.

sfast

6:14 pm on Oct 31, 2007 (gmt 0)

10+ Year Member



To calculate distance from city to city -
I use a database of zipcode where in a city there are obviously more than one zipcode with latitude and longitude information.

How do I know which one to use to calculate the distance knowing that distances are calculated from the heart of the city?

whoisgregg

6:47 pm on Oct 31, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I believe you can simply use the lowest numbered zip code as your city center... At least in the few cities I just checked, it tends to be what would be considered the city center. (So if you have 90210, 90211, and 90212, just use the latitude and longitude of 90210.)

If that doesn't work, you could average all the different coordinates you have to get a "center point" for the city. It won't be perfect, but it should do the trick. :)

sfast

9:09 pm on Oct 31, 2007 (gmt 0)

10+ Year Member




The distance calculation between online center point of the city and the lowest zipcode corodinates was ~6 miles.

So as u suggest, I will go with taking the avg of coordinates.
Thanks