Jump to content


Photo

Determining distance between two cities


  • Please log in to reply
13 replies to this topic

#1 Mateobus

Mateobus
  • Members
  • PipPipPip
  • Advanced Member
  • 95 posts

Posted 11 September 2006 - 07:28 PM

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.

#2 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 11 September 2006 - 07:40 PM

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/

Info: PHP Manual


#3 Mateobus

Mateobus
  • Members
  • PipPipPip
  • Advanced Member
  • 95 posts

Posted 11 September 2006 - 07:43 PM

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.

#4 Mateobus

Mateobus
  • Members
  • PipPipPip
  • Advanced Member
  • 95 posts

Posted 11 September 2006 - 07:49 PM

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/...Los Angeles, 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.

#5 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 11 September 2006 - 07:49 PM

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.

Info: PHP Manual


#6 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 11 September 2006 - 07:50 PM

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.

Info: PHP Manual


#7 Mateobus

Mateobus
  • Members
  • PipPipPip
  • Advanced Member
  • 95 posts

Posted 11 September 2006 - 07:52 PM

thanks ober, ill get to work

#8 Mateobus

Mateobus
  • Members
  • PipPipPip
  • Advanced Member
  • 95 posts

Posted 11 September 2006 - 08:22 PM

Problem with Fopen:
[function.fopen]: failed to open stream: Permission denied

Do i need to modify the php.ini file?

#9 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 11 September 2006 - 08:29 PM

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

Info: PHP Manual


#10 Mateobus

Mateobus
  • Members
  • PipPipPip
  • Advanced Member
  • 95 posts

Posted 11 September 2006 - 08:35 PM

fopen(http://www.mapquest....k&2s=IL&2z=&r=f)

#11 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 11 September 2006 - 11:46 PM

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:
<?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;
}?>

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

Ken

#12 Mateobus

Mateobus
  • Members
  • PipPipPip
  • Advanced Member
  • 95 posts

Posted 11 September 2006 - 11:58 PM

Road Distance is preferred, also I don't know how to get the lat and longitute, I only have city names and states

#13 kid_drew

kid_drew
  • Members
  • PipPipPip
  • Advanced Member
  • 65 posts

Posted 12 September 2006 - 04:01 AM

Getting lat and long is not hard.  There are plenty of free geocoders out there.  Google Map's API will do it.

#14 Mateobus

Mateobus
  • Members
  • PipPipPip
  • Advanced Member
  • 95 posts

Posted 12 September 2006 - 05:02 AM

how would i plug into google maps' API?




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users