Measure distance from multiple points using Google

Asked

Viewed 1,034 times

0

I’m working on an app where the user can see the closest establishments to it.

In the bank I keep the latitude and the longitude, and in mobile I recover the user’s position.

I made a LIMIT in SQL to bring paginated this data ordering by the shortest distance. For this I made use of an algorithm to calculate these points.

However the customer informed me that the distance informed in the app and the distance on google maps are not exact. Analyzing checked that Google does the calculation based on the routes.

How could I cross-reference this data using some google API keeping my sort that is in SQL?

I’ve thought about putting a column in the database and updating but it’s a lot of data for me to keep calling a web-service. Does anyone know any solution to solve this "problem"?

  • You want the distance based on the route or the distance between 2 points "GPS" (so, straight line) IE, a query like "I want all stores that are less than 350m from my ref point." ?

  • Hello Peter I managed to solve... It was right at the beginning of the project, but it was distance of points same

2 answers

1

Even if solved, here an SQL function that I use to calculate distance between points.

NO SQL
DETERMINISTIC
BEGIN RETURN FLOOR(1000*(6371*2*ASIN(SQRT(POWER(SIN((src_lagps - dest_lagps)*pi()/180/2),2) + COS(src_lagps * pi()/180)*COS(dest_lagps*pi()/180)*POWER(SIN((src_logps - dest_logps)*pi()/180/2),2))))); 
END

The parameters: src_lagps, src_logps, dest_lagps and dest_logps are float type. The result is the distance in meter.

Example:

$query_select = "SELECT 
calcul_distance (".$lagps_usuario.",".$logps_usuario.",tloja_lagps,tloja_logps) as tloja_distancia,
tloja_id,
tloja_nom
FROM TAB_LOJA

HAVING tloja_distancia < ".$distancia_maxima." ORDER BY tloja_distancia";

Note that you should use "HAVING" and not "SELECT".

0

It’s been a while since I worked with this kind of calculation, but when I did, I used the Google Distance Matrix.

With it you can define some metrics to make this calculation, as the mode of transport (car, walking, subway, etc.) or route restrictions (avoid avenues, etc..).

What I really liked when I used was the option to use both coordinates (Lat,Lng) and addresses (since valid). As I haven’t used it for a while I don’t know if it has changed, but it was possible to define only one origin and some destinations, so it is only you reconcile the two data (distance and your order) to display the best results.

Note: I needed to enable access to this API within the Google Developers Console.

Browser other questions tagged

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