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.

Was it helpful?

Solution

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.

OTHER TIPS

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
scroll top