Jump to content

Archived

This topic is now archived and is closed to further replies.

Mateobus

Determining distance between two cities

Recommended Posts

I run a college soccer website, and I am trying to write a script that will determine whether or not a game is defined as "in-region" or not.  There are 3 criteria which are easy to determine, however there is one that is not.  THis criteria is: "a game is defined as in-region if the 2 opponents are within 200 miles of each other."  I have the cities and states of the teams programmed in a MySQL database, does anyone know of a fast way of determing the distance between 2 cities easily.  Maybe I could some how plug them into google maps or something... Any creative ideas are greatly appreciated.

Share this post


Link to post
Share on other sites
If you're not going to do it through PHP or some calculated manner, google maps or any mapping program would work fine.  If you could somehow get a longitude/latitude reading for each of these cities, you could calculate it on the fly.

This is kinda cool: http://www.indo.com/distance/

Share this post


Link to post
Share on other sites
I plan on using PHP, the problem is that there is no getDistance(city1, city2) built in php function.  With more than 6,000 games It is not practical to open google maps for every single game.  Also, this does not need to be on the fly.

Share this post


Link to post
Share on other sites
OK, here is one idea, check out this site:
http://www.indo.com/distance/
I can change the URLs, ie if I want the distance between Chicago, and LA, the URL would look like this:
http://www.indo.com/cgi-bin/dist?place1=Chicago%2C+IL&place2=Los+Angeles%2C+CA
This returns a page with the distance somewhere in there.  Does anyone know a way that I could some how save this html file with php, and then parse it to find the distance.  I know this is really complicated, but its a cool idea.

Share this post


Link to post
Share on other sites
You wouldn't have to open it for every one.. you just need to pull out the results in the background.  You can send variables to website and parse through the results of what it sends back and get your data.

Share this post


Link to post
Share on other sites
That's not hard at all... you basically use fopen() to read the file and get down and dirty with arrays and regex.  Try to put something together and then post it when you get stuck.

Share this post


Link to post
Share on other sites
Problem with Fopen:
[function.fopen]: failed to open stream: Permission denied

Do i need to modify the php.ini file?

Share this post


Link to post
Share on other sites
That would be permission denied on the host machine... not yours.  What file are you trying to open?

Share this post


Link to post
Share on other sites
fopen(http://www.mapquest.com/directions/main.adp?go=1&do=nw&rmm=1&un=m&cl=EN&qq=hltF3hzNT9tNhURP0HLlhh9UYBmHRqyBceg4Gkon14D8uewLk7pjHQ%253d%253d&ct=NA&rsres=1&1y=US&1ffi=&1l=&1g=&1pl=&1v=&1n=&1pn=&1a=&1c=Chicago&1s=IL&1z=&2y=US&2ffi=&2l=&2g=&2pl=&2v=&2n=&2pn=&2a=&2c=Highland+Park&2s=IL&2z=&r=f)

Share this post


Link to post
Share on other sites
Do you need actual road distance or will straight line distance do. If straght line distance is ok and you know or can get the longitude & latitude of the city, then there is a formula that can be applied to get the distance.

I found the following on the web about a year and a half ago:
[code]<?php
function distance($lat1, $lon1, $lat2, $lon2, $unit='M') {

  $theta = $lon1 - $lon2;
  $dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) +  cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
  $dist = acos($dist);
  $dist = rad2deg($dist);
  $miles = $dist * 60 * 1.1515;
  switch (strtoupper($unit)) {
  case 'K':
$ret = $miles * 1.609344;
break;
case 'N':
$ret = $miles * 0.8684;
break;
default:
$ret = $miles;
  }
  return $ret;
}?>[/code]

Actually, it may have been written in Javascript, but it was easy to convert to PHP.

Ken

Share this post


Link to post
Share on other sites
Road Distance is preferred, also I don't know how to get the lat and longitute, I only have city names and states

Share this post


Link to post
Share on other sites
Getting lat and long is not hard.  There are plenty of free geocoders out there.  Google Map's API will do it.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.