OpenSourceBikeShare
OpenSourceBikeShare copied to clipboard
Geofence
Only allow the user to rent / return a bike if they are within x meters of the stand.
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));
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);
}