2

I want to make a location-based academic website(bootstrap project ) where a said question will be visible only to those present in the pre-specified radius. I can code in HTML,CSS and currently learning JavaScript and PHP. Anyone willing to give me some pointers? (code resources, relevant templates or useful literature)

  • This seems like a big project. If you want to do this properly I recommend looking into php frameworks (symfony2 for example). In this example you can get the user's ip adress: http://stackoverflow.com/questions/9029757/how-do-i-get-the-user-ip-address-in-symfony2-controller and with the IP you can find their location. Once you have their location you can send back the corresponding view ( or a json if you are doing this via xmlhttprequest (ajax) ) containing the question's information. – kemicofa ghost Apr 18 '15 at 21:47
  • I have a month(16 hours a day) to do this, hopefully it's enough.For front end, I am looking for a simple, free sourcecode template to reduce the overall work as functionality takes precedence. Thanks a lot, Grimbode. – Muhammad Ali Apr 19 '15 at 01:37

1 Answers1

0

You can find the distances beteen locations using the Haversine formula. The following javascript functions are used to calculate the distance between the geolocated coordinates and that of the center location. Only if the distance is less than radius allow access.

function deg2rad(degrees){
radians = degrees * (Math.PI/180);
return radians;
}

function Haversine(lat1,lon1,lat2,lon2) {
  deltaLat = lat2 - lat1 ;
  deltaLon = lon2 - lon1 ;
  earthRadius = 3959; // in miles 6371 in meters.
  alpha    = deltaLat/2;
  beta     = deltaLon/2;
  a        = Math.sin(deg2rad(alpha)) * Math.sin(deg2rad(alpha)) + Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * Math.sin(deg2rad(beta)) * Math.sin(deg2rad(beta)) ;
  c        = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
  distance =  earthRadius * c;
  return distance.toFixed(2);
}
david strachan
  • 7,174
  • 2
  • 23
  • 33