Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

varovas's avatar

Search with location radius

Hi guys, I was wondering how can I make search in a website with location radius. For example: I am making ads website, where you can search for some selling cars. And for example I want to search for cars in London and a 30 miles around London. How can I make that kind of search?

0 likes
10 replies
tisuchi's avatar

You can use DB::Select in this case.

For example-

DB::select('SELECT * FROM
                    (SELECT id, name, address, phone, latitude, longitude, (' . $circle_radius . ' * acos(cos(radians(' . $lat . ')) * cos(radians(latitude)) *
                    cos(radians(longitude) - radians(' . $lng . ')) +
                    sin(radians(' . $lat . ')) * sin(radians(latitude))))
                    AS distance
                    FROM users) AS distances
                WHERE distance < ' . $max_distance . '
                ORDER BY distance
                OFFSET 0
                LIMIT 20;
            ');
varovas's avatar

Using this method I will have to add distances to database by myself. Maybe there is some method or google maps integration, that estimate distances automatically?

tekmi's avatar

I see here two possible implementations:

You don’t posses enough information about your data (no latitude and longitude information)

I would firstly advise to scan Google’s Geocoding API: https://developers.google.com/maps/documentation/geocoding/start

This API will help you with converting your location name (e.g London) into the Latitude and Longitude pair, which will be the base of the future searches around this particular point.

Having this, you may be further interested in checking out https://developers.google.com/places/web-service/search, which will harvest the extensive Google Places database and return you the specific places within given radius.

You have detailed information about your data sets (latitude and longitude provided)

In this case it’s a matter of how you will persist your location data. You could use simple float columns, you could use database SPATIAL extensions or you could turn your efforts into Full Text Search solutions, like Elasticsearch

I would go with the last option, considering the fact that Elasticsearch offers pretty powerful filters for geo-queries: https://www.elastic.co/guide/en/elasticsearch/reference/current/geo-queries.html

I would also recommend reading Elasticsearch: The Definitive Guide http://shop.oreilly.com/product/0636920028505.do especially part V, Geolocation.

1 like
kobear's avatar

@varovas no, you do not need to know the distance each place is from each other. the distance column is calculated by the trigonometry in the SQL select statement, and the $max_distance and $circle_radius are the limiters you use to control the query. So, you set $circle_radius to 30 miles to get anything in a 30 mile radius, and set $max_distance to 30 miles if you want the total distance to a location to not exceed 30 miles.

regan's avatar

Hi TISUCHI, Can you tell me if exactly the same code can be used for MILES and KM ?? Or if some adjustments are required can you suggest what?? Thanks! :)

tisuchi's avatar

Actually this is the formula that Google provides us.

2 likes
Sinnbeck's avatar

@mengthong If you are having problems, then please make a new thread with the issue. I doubt the error is related to laravel, but rather your mysql instance.

2 likes
tisuchi's avatar

@mengthong Actually no idea now. It's been 4 years.

I totally agree with @sinnbeck. You better create a new thread about it to get the appropriate response.

1 like

Please or to participate in this conversation.