Reputation: 3105
I have found plenty of demos and examples on how to catch someones geolocation, but am having a hard time figuring out how to take those coordinates and compare them to a coordinates of a building (or I guess to make it more of a catch all; compare their current approximate coordinates to the coordinates of a building).
Won't post any code regarding how to find someones position as that is everywhere, but my current end result is that I have the long/lat coordinates of my device, but what I need to do now is compare them to the coordinates of a specific buildings coordinates, then perform an action if they are within say, 1000 metres (redirect to a page.. yeah, I know). I am currently using getCurrentPosition but I realize I might have to switch to watchPosition after researching the differences.
Any help on comparing coordinates, or coordinates + estimated area would be very helpful..
Thanks
Upvotes: 3
Views: 2040
Reputation: 5687
You'll probably want to use the Haversine formula for calculating distance, but if you are only interested in very small distances, you might just use quadratic
Here is an example of the Haversine via MYSQL: https://developers.google.com/maps/articles/phpsqlsearch_v3#findnearsql (if your various positions happen to be in a DB...but if you really just have a single building, the formula still holds, it's just that you'll implement it in your JS rather than in the query)
http://www.movable-type.co.uk/scripts/latlong.html
http://en.wikipedia.org/wiki/Haversine_formula
Upvotes: 2