⬆️ ⬇️

Find neighbors on Google Maps

I looked at the statistics of visiting the site of the Ulyanovsk Cafe and realized that about 30% of visitors come to us through a search engine to a page with a specific description of the cafe, get the information they need and go away.

“Disorder” we decided and set a new task - to display on the cafe page the nearest establishments.

In fact, there were 2 goals:

1. gently suggest to walk around the site.

2. give people a choice if they don't like the cafe

But the nearest institution is a vague concept. They clarified, it turned out that according to the majority, the “nearest ones” are within walking distance of a couple of minutes (so as not to overpark the car).

2 minutes of walking (at a speed of 5 km / h) is 170 meters. Rounded to 200 (which is already there). And they came up against a new problem - how to accurately “hang in meters”, if we only have the latitude and longitude of the site.

A bit of math for the 5th grade, and we were able to convert degrees to meters for Ulyanovsk.

$sql="

SELECT *,

sqrt(POWER(((lat-".$row['lat'].")*110349.7867154), 2) + POWER(((lng-".$row['lng'].")*64505.305504), 2)) as `distance`

FROM `cafe`

WHERE

sqrt(POWER(((lat-".$row['lat'].")*1110349.7867154), 2) + POWER(((lng-".$row['lng'].")*64505.305504), 2)) < 200

ORDER BY `distance` ASC

";




UPDATE:

lat and lng - the names of the corresponding fields in the table

$ row ['lat'] and $ row ['lng'] - coordinates of the point around which we are looking

110349.7867154 - coefficient converting latitude degrees to meters for Ulyanovsk

64505.305504 - coefficient converting degrees of longitude to meters for Ulyanovsk



UPDATE2:

thank you kandy and homm . Thanks to their suggestions on a test machine, the request is processed 2 times faster (0.0040 instead of 0.0084)

before request we optimize a little

$distanceSq = $distance^2;

$row['latMin'] = $row['lat'] - 200/1110349.7867154;

$row['latMax'] = $row['lat'] + 200/1110349.7867154;

$row['lngMin'] = $row['lng'] - 200/64505.305504;

$row['lngMax'] = $row['lng'] + 200/64505.305504;



$sql="

SELECT *,

sqrt(POWER(((lat-".$row['lat'].")*110349.7867154), 2) + POWER(((lng-".$row['lng'].")*64505.305504), 2)) as `distance`

FROM `cafe`

WHERE

(lat >= ".$row['latMin']." AND lat <= ".$row['latMax'].")

AND (lng >= ".$row['lngMin']." AND lng <= ".$row['lngMax'].")

AND POWER(((lat-".$row['lat'].")*1110349), 2) + POWER(((lng-".$row['lng'].")*64505), 2) < ".$distanceSq."

ORDER BY `distance` ASC

";




This query retrieves a list of the nearest tokens from the database. Despite its cumbersome, it works on our table of 150 institutions in just 0.004 seconds .

You can see the result: Restaurant, cafe, bar Comrade Sukhov in Ulyanovsk under the photos. The beauty has not been put on this list yet, sorry :)



')

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



All Articles