cyklokoalicia / OpenSourceBikeShare

The world's first low-cost and open source bike sharing system. (new version in development, use working "breakthrough" release instead!)
http://opensourcebikeshare.com/
GNU General Public License v3.0
167 stars 71 forks source link

Geofence #131

Open ewooonk opened 6 years ago

ewooonk commented 6 years ago

Only allow the user to rent / return a bike if they are within x meters of the stand.

ewooonk commented 6 years ago

The following code can be used to calculate the distance (in km) between the user and the stand in functions.js. Next step: implementing in rent() function.

latdif = Math.abs($("body").data("mapcenterlat")-lat); 
longdif = Math.abs($("body").data("mapcenterlong")-long);
latdifkm = latdif*111;
longdifkm = longdif*111*Math.cos(lat*Math.PI/180);
difkm = Math.sqrt((latdifkm*latdifkm)+(longdifkm*longdifkm));
ewooonk commented 6 years ago

Full implementation of the geofence.

functions.js

showlocation, changelocation Change the radius of the circle displayed on the map to 200m. circle = L.circle([$("body").data("mapcenterlat"), $("body").data("mapcenterlong")],40*5, {

showstand Add:

latdif = Math.abs($("body").data("mapcenterlat")-lat);
longdif = Math.abs($("body").data("mapcenterlong")-long);
latdifkm = latdif*111;
longdifkm = longdif*111*Math.cos(lat*Math.PI/180);
difkm = Math.sqrt((latdifkm*latdifkm)+(longdifkm*longdifkm));

rent

Add:

   geofenceid=1;
   if (difkm < 0.2) geofenceid=2;

Replace:

url: "command.php?action=rent&bikeno="+$('#rent .bikenumber').html()+"&geofence="+geofenceid

returnbike

Add:

   geofenceid=1;
   if (difkm < 0.2) geofenceid=2;

Replace:

url: "command.php?action=return&bikeno="+$('#return .bikenumber').html()+"&stand="+standname+note+"&geofence="+geofenceid

command.php

Replace

   case "rent":
      logrequest($userid,$action);
      checksession();
      $bikeno=trim($_GET["bikeno"]);
      $geofenceid=trim($_GET["geofence"]);
      checkbikeno($bikeno);
      rent($userid,$bikeno,$geofenceid);
      break;
   case "return":
      logrequest($userid,$action);
      checksession();
      $bikeno=trim($_GET["bikeno"]);
      $stand=trim($_GET["stand"]);
      $geofenceid=trim($_GET["geofence"]);
      $note="";
      if (isset($_GET["note"])) $note=trim($_GET["note"]);
      checkbikeno($bikeno); checkstandname($stand);
      returnBike($userid,$bikeno,$stand,$note,$geofenceid);
      break;

Replace:

   case "forcerent":
      logrequest($userid,$action);
      checksession();
      checkprivileges($userid);
      $bikeno=trim($_GET["bikeno"]);
      checkbikeno($bikeno);
      rent($userid,$bikeno,$geofence,TRUE);
      break;
   case "forcereturn":
      logrequest($userid,$action);
      checksession();
      checkprivileges($userid);
      $bikeno=trim($_GET["bikeno"]);
      $stand=trim($_GET["stand"]);
      $note="";
      if (isset($_GET["note"])) $note=trim($_GET["note"]);
      checkbikeno($bikeno); checkstandname($stand);
      returnBike($userid,$bikeno,$stand,$note,$geofenceid,TRUE);
      break;

actions-web.php

rent

Replace

function rent($userId,$bike,$geofenceidd,$force=FALSE)

Add the following to if ($force==FALSE)

      if ($geofenceidd==1)
         {
         response(_('You are outside the geofence.')." "._('Please, enable GPS location services or choose a nearby stand.'),ERROR);
         }

returnBike

Replace

'function returnBike($userId,$bike,$stand,$note="",$geofenceidd,$force=FALSE)'

Add the following to if ($force==FALSE)

      if ($geofenceidd==1)
         {
         response(_('You are outside the geofence.')." "._('Please, enable GPS location services or choose a nearby stand.'),ERROR);
         }