So I've done a little looking around and couldn't find anything that really answered what I'm trying to do, thus I'm posting!
My overall aim is essentially for the page to read the users location and then run code based on where they are. To be specific I have a facebook checkin script that will allow the user to check in if they're in a specific location.
The issue is the location in question is kinda big so manually putting in the coordinates of the location doesn't work. What I'm now stuck with is whether it's possible to tell JS to take the hard coded longitude and latitude of the location but give a radius around the coordinates (lets say 200 meters) so when the user enters the 200m radius of the coordinate the code activates.
Does anyone have any ideas?
This is my code so far.
jQuery(window).ready(function(){
initiate_geolocation();
});
function initiate_geolocation() {
navigator.geolocation.getCurrentPosition(handle_geolocation_query,handle_errors);
}
function handle_errors(error)
{
switch(error.code)
{
case error.PERMISSION_DENIED: alert("user did not share geolocation data");
break;
case error.POSITION_UNAVAILABLE: alert("could not detect current position");
break;
case error.TIMEOUT: alert("retrieving position timed out");
break;
default: alert("unknown error");
break;
}
}
function handle_geolocation_query(position){
var lat = position.coords.latitude;
var long = position.coords.longitude;
//these are for testing purposes
alert('Your latitude is '+lat+' and longitude is '+long);
if (lat == 0 && long == 0) {alert('It works!');};
}
So I've done a little looking around and couldn't find anything that really answered what I'm trying to do, thus I'm posting!
My overall aim is essentially for the page to read the users location and then run code based on where they are. To be specific I have a facebook checkin script that will allow the user to check in if they're in a specific location.
The issue is the location in question is kinda big so manually putting in the coordinates of the location doesn't work. What I'm now stuck with is whether it's possible to tell JS to take the hard coded longitude and latitude of the location but give a radius around the coordinates (lets say 200 meters) so when the user enters the 200m radius of the coordinate the code activates.
Does anyone have any ideas?
This is my code so far.
jQuery(window).ready(function(){
initiate_geolocation();
});
function initiate_geolocation() {
navigator.geolocation.getCurrentPosition(handle_geolocation_query,handle_errors);
}
function handle_errors(error)
{
switch(error.code)
{
case error.PERMISSION_DENIED: alert("user did not share geolocation data");
break;
case error.POSITION_UNAVAILABLE: alert("could not detect current position");
break;
case error.TIMEOUT: alert("retrieving position timed out");
break;
default: alert("unknown error");
break;
}
}
function handle_geolocation_query(position){
var lat = position.coords.latitude;
var long = position.coords.longitude;
//these are for testing purposes
alert('Your latitude is '+lat+' and longitude is '+long);
if (lat == 0 && long == 0) {alert('It works!');};
}
Share
Improve this question
edited Mar 1, 2013 at 23:15
Purify
asked Mar 1, 2013 at 23:02
PurifyPurify
5892 gold badges12 silver badges26 bronze badges
2
-
1
By the way, the second to last line in your code sets both lat and long to 0 and I doubt that's what you mean to do:
if (lat = 0 && long = 0) {alert('It works!');};
should probably beif (lat == 0 && long == 0) {alert('It works!');};
– Mara Ormston Commented Mar 1, 2013 at 23:08 - Good catch! I didn't see this. I had edited it to 0 from the actual coordinates so in case people think I'm strange for pointing to 0/0, I'm not. :p – Purify Commented Mar 1, 2013 at 23:14
1 Answer
Reset to default 14What I would do is create a poll function with setInterval, do it every 1s to 10s depending on what makes the most sense for your testing, and just test distance. Here's a function to test distance between two longitudes/latitudes:
function CalculateDistance(lat1, long1, lat2, long2) {
// Translate to a distance
var distance =
Math.sin(lat1 * Math.PI) * Math.sin(lat2 * Math.PI) +
Math.cos(lat1 * Math.PI) * Math.cos(lat2 * Math.PI) * Math.cos(Math.abs(long1 - long2) * Math.PI);
// Return the distance in miles
//return Math.acos(distance) * 3958.754;
// Return the distance in meters
return Math.acos(distance) * 6370981.162;
} // CalculateDistance
Your interval function would look something like:
// The target longitude and latitude
var targetlong = 23.456;
var targetlat = 21.098;
// Start an interval every 1s
var OurInterval = setInterval(OnInterval, 1000);
// Call this on an interval
function OnInterval() {
// Get the coordinates they are at
var lat = position.coords.latitude;
var long = position.coords.longitude;
var distance = CalculateDistance(targetlat, targetlong, lat, long);
// Is it in the right distance? (200m)
if (distance <= 200) {
// Stop the interval
stopInterval(OurInterval);
// Do something here cause they reached their destination
}
}