PHP find addresses up to 5km away [duplicate]

2

Hello.

I'm setting up a system to find the nearest service provider on the zip.

For this I used the script below to convert zip into coordinate.

$cep = "01238010";
$geocode = file_get_contents('http://maps.google.com/maps/api/geocode/json?address='.$cep.'&sensor=false');
$output= json_decode($geocode);
$lat = $output->results[0]->geometry->location->lat;
$long = $output->results[0]->geometry->location->lng;

How do I find providers that are within 5km of this coordinate?

I have a mysql database with the ceps and registered coordinates of the providers.

I'm using PHP and Mysql.

Thank you!

    
asked by anonymous 25.05.2017 / 15:43

1 answer

0

Use the MySQL spatial functions

If your MySQL is version 5.6 or higher, by using the database's own distance calculation function.

Example in the MySQL site :

mysql> SET @g1 = Point(1,1);
mysql> SET @g2 = Point(2,2);
mysql> SELECT ST_Distance(@g1, @g2);
+-----------------------+
| ST_Distance(@g1, @g2) |
+-----------------------+
|    1.4142135623730951 |
+-----------------------+

In this article the author also puts a very complete example of how it works.

    
25.05.2017 / 19:45