In most of the online store websites are providing the feature to check the delivery to your zip code. You only have to enter your shipping zip code to check whether product can be delivered to your address or not. If you are a developer then I think some questions must arise in your mind that how this online store checks the delivery and calculates the cost of the shipping. Most of the shipping process calculates the shipping cost and delivery check by calculating the distance between the pick-up points to the shipping point with some other things like weights, size etc. So today we will learn to calculate the distance between the two zip codes in PHP. This is very simple to calculate the distance through google API.
Lets take an example
Pick-up point zip code – 201301
shipping zip code – 110044
you have to call an google API by passing this zip codes one by one then have to get the latitude and longitude of both the zip codes.
here is the pick-up point zip code latitude and longitude
here is the shipping point zip code latitude and longitude
Then calculate the distance in km
So the Distance is 9 km .
$lat1 = ‘28.5821195’;
$lon1 = ‘77.3266991’;
$lat2 = ‘28.5006897’;
$lon2 = ‘77.3152116’;
$unit = ‘km’;
$theta = $lon1 – $lon2;
$dist = sin(deg2rad((double)$lat1)) * sin(deg2rad((double)$lat2)) + cos(deg2rad((double)$lat1)) + cos(deg2rad((double)$lat2)) * cos(deg2rad((double)$theta));
$dist = acos($dist);
$dist = rad2deg($dist);
$distance = round($dist * 60 * 1.1515 * 1.609344);
echo $distance;
$distance return NAN output.