Calculating distance between zip codes in PHP
You can also try hitting a web service to calc the distance. Let someone else do the heavy lifting.
https://www.zipcodeapi.com/API#distance
Calculating driving distance using zip codes
There isn't a whole lot out there that's free, other than Google Maps. The MapQuest API may give you what you need. Here's their guide to the service - it addresses getting driving distances/times between a set of points. Their terms of service don't appear to require that you display a map, but take a look and see if that meets your needs.
Zip Code proximity calculation in PHP
You should download Popular Data's ZIP code database.
With this, you can use the following to calculate distance.
function calc_distance($point1, $point2)
{
$radius = 3958; // Earth's radius (miles)
$deg_per_rad = 57.29578; // Number of degrees/radian (for conversion)
$distance = ($radius * pi() * sqrt(
($point1['lat'] - $point2['lat'])
* ($point1['lat'] - $point2['lat'])
+ cos($point1['lat'] / $deg_per_rad) // Convert these to
* cos($point2['lat'] / $deg_per_rad) // radians for cos()
* ($point1['long'] - $point2['long'])
* ($point1['long'] - $point2['long'])
) / 180);
return $distance; // Returned using the units used for $radius.
}
Above was stolen from Adam Bellaire's answer in Calculating distance between zip codes in PHP. After doing this calculation, you can simply sort the results.
Calculate Distance Between Two Postcodes In PHP
If you take a look at the result from google you will notice that it is calculating a route from india to russia.
"destination_addresses" : [ "Wologda, Oblast Wologda, Russland, 160017" ],
"origin_addresses" : [ "Punjab 144216, Indien" ],
This happens because you only provided postal codes, without reference from which country they are. I assume you want to calculate a route within india, since your profile shows that you are from there.
If you just add ,india
to your postal codes, like this:
http://maps.googleapis.com/maps/api/distancematrix/json?origins=144216,india&destinations=160017,india&mode=driving&language=en-EN&sensor=false
You get a result that looks much more like what you want:
{
"destination_addresses" : [ "Chandigarh, 160017, India" ],
"origin_addresses" : [ "Punjab 144216, India" ],
"rows" : [
{
"elements" : [
{
"distance" : {
"text" : "201 km",
"value" : 200693
},
"duration" : {
"text" : "3 hours 30 mins",
"value" : 12625
},
"status" : "OK"
}
]
}
],
"status" : "OK"
}
Fastest way (and most optimized) to calculate and sort users by ascending order of zip code distance
As I'm not overly familiar with PHP or MySQL, I can only give some basic tips but they should help. This also assumes you have no direct way of interfacing with the zip library from MySQL.
First, as it's doubtful that you have 10k zip codes in a city, take your existing query and do something like
SELECT DISTINCT ZipCode FROM Users WHERE ...
This will probably return a few dozen zip codes max, and no duplicates. Run this through your zip code library. That library itself is probably a source of slowness, as it has to look up the zip codes, and do a bunch of fancy trig to get actual distance. Take the results of this, and insert it into a temp table with just the zip code and the distance.
Once done with that list, have another query that gets the rest of the user data you want, and JOIN into the the temp table on zip code to get your distance.
This should give you quite a large speedup. You can do whatever paging you need in the second query after the results have been calculated. And no more looping through 10k rows.
Related Topics
How to Send Email from Localhost Using PHP on Linux
PHP Inserting Multiple Checkbox and Textbox Arrays into MySQL Database
Composer Killed While Updating
Differencebetween Sessions and Cookies in PHP
Handle Fatal Errors in PHP Using Register_Shutdown_Function()
Can You Re-Populate File Inputs After Failed Form Submission with PHP or JavaScript
Why am I Getting Mime-Type of .CSV File as "Application/Octet-Stream"
Str_Replace() on Multibyte Strings Dangerous
How to Open a Link in Private Browsing Mode
Retrieve the Id of an Inserted Record: PHP & Ms SQL Server
How to Enable MySQLi Support for PHP's Cli
How to Force Users to Access My Page Over Https Instead of Http
Autoloading Classes in PHPunit Using Composer and Autoload.Php
How to Kill A/All PHP Sessions
Will Enabling Xdebug on a Production Server Make PHP Slower
Can PHP and ASP.NET Run Together Within the Same Web Site in Iis 7.5