Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
201 views
in Technique[技术] by (71.8m points)

mysql - How to calculate driving distance between two latitude and longitudes in PHP

I have a database which contains many latitude and longitudes. I need to get all the latitudes and longitudes sorted in ascending order by distance with respect to a latitude and longitude which I can set manually. I used a formula to solve this problem which is:

SELECT id, ( 3959 * acos( cos( radians(37) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(-122) ) + sin( radians(37) ) * sin( radians( lat ) ) ) ) AS distance 
FROM markers HAVING distance < 25 ORDER BY distance LIMIT 0 , 20;

37 and -122 are the latitudes and longitudes and it will return all the values in a field named distance in a sorted way. My problem is that's not my necessity if a spot is 2 km away but driving distance is 11 km. I need a formula based on google maps which will calculate and return the driving distance between two coordinates.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can calculate the straight line distance, you can't "calculate" the driving distance without a routing database/engine.

If you need the driving distance, you can use either the Google directions API (for a single route, potentially with waypoints) or the Google distance matrix API (can provide results for multiple origin/destinations)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...