Final distance (miles) should be two places using zipcode

We have 100 users in our system. At registration, they entered their zipcodes. Now I need me to enter any zip code, should it give me a result with the distance between the entered zipcode and the other 100 zipcode users?

Can this be done, help me if anyone finds out about solutions?

+4
source share
2 answers

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

+2
source

There is a Zip Code API that does this - http://zipcodedistanceapi.redline13.com/API .

0
source

Source: https://habr.com/ru/post/1495684/


All Articles