PHP find addresses up to 5km away

Asked

Viewed 416 times

2

Hello.

I’m mounting a system to find the nearest service provider to the zip code.

For this I used the script below to convert cep in 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 the providers who are within 5km of that coordinate?

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

I’m using PHP and Mysql.

Thank you!

  • vc will have you set up a logarithm , maybe a mysql file. the account is this one . http://www.pilotopolicial.com.br/calculando-distancias-e-direcoes-utilizando-coordenadas-geograficas/

  • In your Mysql database are the positions on the map of the providers using spatial data type? (POINT)

1 answer

0


Use Mysql space functions

If your Mysql is version 5.6 or higher, why use the distance calculation function from the database itself.

Example in mysql website:

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 that article the author also puts a very complete example of how it works.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.