I would do this in two parts:
- A geocoding script is run once and the results are stored in a persistent cache (for example, a database). This way you will avoid speed limits and speed up your final search.
- Script to calculate the distance, execute if necessary, or cache it too, to build a lookup table that stores the distances between each zip code and everyone else. Since you have only 100 zip codes, this lookup table will not be very large.
Geocoding
<?php // Script to geocode each ZIP code. This should only be run once, and the // results stored (perhaps in a DB) for subsequent interogation. // Note that google imposes a rate limit on its services. // Your list of zipcodes $zips = array( '47250', '43033', '44618' // ... etc ... ); // Geocode each zipcode // $geocoded will hold our results, indexed by ZIP code $geocoded = array(); $serviceUrl = "http://maps.googleapis.com/maps/api/geocode/json?components=postal_code:%s&sensor=false"; $curl = curl_init(); foreach ($zips as $zip) { curl_setopt($curl, CURLOPT_URL, sprintf($serviceUrl, urlencode($zip))); curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); $data = json_decode(curl_exec($curl)); $info = curl_getinfo($curl); if ($info['http_code'] != 200) { // Request failed } else if ($data->status !== 'OK') { // Something happened, or there are no results } else { $geocoded[$zip] =$data->results[0]->geometry->location; } }
Distance calculation
As Mark says, there are good examples for this, such as Measuring the distance between two coordinates in PHP
source share