Mateobus Posted September 11, 2006 Share Posted September 11, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/20416-determining-distance-between-two-cities/ Share on other sites More sharing options...
ober Posted September 11, 2006 Share Posted September 11, 2006 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/ Quote Link to comment https://forums.phpfreaks.com/topic/20416-determining-distance-between-two-cities/#findComment-89950 Share on other sites More sharing options...
Mateobus Posted September 11, 2006 Author Share Posted September 11, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/20416-determining-distance-between-two-cities/#findComment-89951 Share on other sites More sharing options...
Mateobus Posted September 11, 2006 Author Share Posted September 11, 2006 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+CAThis 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. Quote Link to comment https://forums.phpfreaks.com/topic/20416-determining-distance-between-two-cities/#findComment-89957 Share on other sites More sharing options...
ober Posted September 11, 2006 Share Posted September 11, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/20416-determining-distance-between-two-cities/#findComment-89959 Share on other sites More sharing options...
ober Posted September 11, 2006 Share Posted September 11, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/20416-determining-distance-between-two-cities/#findComment-89961 Share on other sites More sharing options...
Mateobus Posted September 11, 2006 Author Share Posted September 11, 2006 thanks ober, ill get to work Quote Link to comment https://forums.phpfreaks.com/topic/20416-determining-distance-between-two-cities/#findComment-89963 Share on other sites More sharing options...
Mateobus Posted September 11, 2006 Author Share Posted September 11, 2006 Problem with Fopen:[function.fopen]: failed to open stream: Permission deniedDo i need to modify the php.ini file? Quote Link to comment https://forums.phpfreaks.com/topic/20416-determining-distance-between-two-cities/#findComment-89990 Share on other sites More sharing options...
ober Posted September 11, 2006 Share Posted September 11, 2006 That would be permission denied on the host machine... not yours. What file are you trying to open? Quote Link to comment https://forums.phpfreaks.com/topic/20416-determining-distance-between-two-cities/#findComment-89996 Share on other sites More sharing options...
Mateobus Posted September 11, 2006 Author Share Posted September 11, 2006 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) Quote Link to comment https://forums.phpfreaks.com/topic/20416-determining-distance-between-two-cities/#findComment-90000 Share on other sites More sharing options...
kenrbnsn Posted September 11, 2006 Share Posted September 11, 2006 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]<?phpfunction 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 Quote Link to comment https://forums.phpfreaks.com/topic/20416-determining-distance-between-two-cities/#findComment-90113 Share on other sites More sharing options...
Mateobus Posted September 11, 2006 Author Share Posted September 11, 2006 Road Distance is preferred, also I don't know how to get the lat and longitute, I only have city names and states Quote Link to comment https://forums.phpfreaks.com/topic/20416-determining-distance-between-two-cities/#findComment-90120 Share on other sites More sharing options...
kid_drew Posted September 12, 2006 Share Posted September 12, 2006 Getting lat and long is not hard. There are plenty of free geocoders out there. Google Map's API will do it. Quote Link to comment https://forums.phpfreaks.com/topic/20416-determining-distance-between-two-cities/#findComment-90200 Share on other sites More sharing options...
Mateobus Posted September 12, 2006 Author Share Posted September 12, 2006 how would i plug into google maps' API? Quote Link to comment https://forums.phpfreaks.com/topic/20416-determining-distance-between-two-cities/#findComment-90212 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.