Jump to content

Determining distance between two cities


Mateobus

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.
Link to comment
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/
Link to comment
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.
Link to comment
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)
Link to comment
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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.