Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Radius of longitude and latitude in HTML5 geolocation

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!');};
    } 
like image 429
Purify Avatar asked Mar 01 '13 23:03

Purify


People also ask

What is the geolocation in HTML5?

Geo-location in HTML5 is used to share the location with some websites and be aware of the exact location. It is mainly used for local businesses, restaurants, or showing locations on the map. It uses JavaScript to give latitude and longitude to the backend server. Most of the browsers support Geolocation API.

How can I get current location using latitude and longitude in HTML?

If supported, run the getCurrentPosition() method. If not, display a message to the user. If the getCurrentPosition() method is successful, it returns a coordinates object to the function specified in the parameter (showPosition) The showPosition() function outputs the Latitude and Longitude.

How does geolocation work in HTML?

How does the HTML5 geolocation API work? HTML5 geolocation detects latitude and longitude coordinates by using the device's GPS (if available on the device) or the device's mobile/WIFI signal (if GPS is not available. The mobile/WIFI signals are triangulated to work out the latitude and longitude.

What is geolocation radius?

It is essentially the mean radius of the planet in meters. If you were to use another planet for these conversions, you would use a different constant. – Mark Ormston. Jul 17, 2018 at 21:01. I needed to divide the distance returned from CalculateDistance by 100 to get the real meters :S.


1 Answers

What 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
  }
}
like image 150
Mark Ormston Avatar answered Sep 28 '22 15:09

Mark Ormston