Q695 Posted February 17, 2014 Share Posted February 17, 2014 Is anyone aware of a good zip code distance calculator that uses the built in one on google maps, or something similar? Quote Link to comment https://forums.phpfreaks.com/topic/286250-zip-code-distance-calculator/ Share on other sites More sharing options...
cyberRobot Posted February 17, 2014 Share Posted February 17, 2014 Have you looked into the Google Distance Matrix API? https://developers.google.com/maps/documentation/distancematrix/ Quote Link to comment https://forums.phpfreaks.com/topic/286250-zip-code-distance-calculator/#findComment-1469315 Share on other sites More sharing options...
Q695 Posted February 18, 2014 Author Share Posted February 18, 2014 I'll be doing it for users within any given zip code, and calculating the distance to other users, so I don't think the tool is powerful enough. Anyone have advice? Quote Link to comment https://forums.phpfreaks.com/topic/286250-zip-code-distance-calculator/#findComment-1469336 Share on other sites More sharing options...
kicken Posted February 18, 2014 Share Posted February 18, 2014 I'll be doing it for users within any given zip code, and calculating the distance to other users, so I don't think the tool is powerful enough. Why would you think that? Just pass the users entered zipcode to the API and read the results. It'll do exactly what you want. Quote Link to comment https://forums.phpfreaks.com/topic/286250-zip-code-distance-calculator/#findComment-1469343 Share on other sites More sharing options...
sKunKbad Posted February 21, 2014 Share Posted February 21, 2014 I have a sqlite database of zip codes and their geo coordinates. When I want to find the distance between two zip codes I query the db and then use the following function: <?php /** * This function calculates the distance between two * points (given the latitude/longitude of those points). * * @param decimal degrees latitude of point 1 * @param decimal degrees longitude of point 1 * @param decimal degrees latitude of point 2 * @param decimal degrees longitude of point 2 * @param string the unit for results * 'M' is statute miles * 'K' is kilometers * 'N' is nautical miles */ if( ! function_exists( 'distance_between_geo_coords' ) ) { function distance_between_geo_coords( $lat1, $lng1, $lat2, $lng2, $unit ) { $theta = $lng1 - $lng2; $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; $unit = strtoupper( $unit ); if( strtolower( $unit ) == 'k' ) { return $miles * 1.609344; } else if( strtolower( $unit ) == 'n' ) { return $miles * 0.8684; } else { return $miles; } } } // -------------------------------------------------------------- Quote Link to comment https://forums.phpfreaks.com/topic/286250-zip-code-distance-calculator/#findComment-1469786 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.