This site (Calculate distance, bearing and more between Latitude/Longitude points) provide a good explanation and calculation formula. You may want to check it out.
How should I calculate if a person is within 50 meters range of a store using geolocation?
-
19-10-2022 - |
Question
I have the store's latitude and longitude saved in database, and the user's geolocation is obtained with javascript from his mobile browser.
How should I calculate if the user's distance from the store is within 50m?
Maybe (x1-x2)**2+(y1-y2)**2 < a certain threshold
?
p.s. This is like foursquare checkin.
Solution
OTHER TIPS
sqrt((x2-x1)^2 + (y2-y1)^2) < 50
http://www.mathwarehouse.com/algebra/distance_formula/index.php
Here is an example using javascript
:
function calcDistance(p1, p2){
return Math.sqrt(Math.pow(p2.x-p1.x, 2) + Math.pow(p2.y-p1.y, 2));
}
var store = { x:21, y:36 };
var device = { x:11, y:56 };
console.log( calcDistance(store, device) );
console.log( "Within 50m ? " + (calcDistance(store, device) < 50) );
Result:
22.360679774997898
Within 50m ? true
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow