How would I go about setting up a tool that would calculate if a user’s address is within a set radius? For example, on an ecommerce admin panel, the store owner would set up a maximum delivery radius which would save the value to a database field.
Once a user goes to checkout, I would need the site to perform a check to see if their address is within the preset delivery radius. If not then the user wouldn’t be able to checkout and pay.
Would it be possible to integrate Google Maps so that the store owner/admin could draw a delivery radius a bit like the image below or would they just have to work with latitude and longitude/distance from co-ordinates?
Yes, i have a site online which uses that method serhat, works well:
sqrt(power(69.1*(lat - “.$baselat.”),2)+power(69.1*(lng - “.$baselng.”)*cos(lat/57.3),2))
where $baselat and $baselng are centre and lat,lng are table fields
(returns miles, not Km as I am in UK)
Implemented before custom queries I may add so it is part of a custom piece of code, not tried it with the new Wappler custom query
Dug out the original custom code snippet
THis is from a student accommodation website where the routine finds student accommodation within a distnace from then ir preferred location, normally a university. All properties are pre-geocoded with fields lat, lng
This student is logged in so the student identity is stored in the security provider ($_SESSION[‘userSecurityId’])
The initial query gets the max distance a student is prepared to travel (in miles, this is in the UK)
Then select part of the statement does the work to find all accommodation providers within that radius and then in this case the results are inserted into a table holding the matches storing the student ID, property ID and the distance from the preferred location
Here is the code snippet (slightly simplified form original)
// login credentials
$username='xxxxxxx';
$pass='xxxxxxxx';
// get users ID for secirity prodider
$user = $_SESSION['userSecurityId'];
$pdo = new PDO('mysql:host=xxxxxxxxxx;dbname=xxxxxxxxxxxxxxxxxxxx;charset=utf8', $username, $pass);
// this gets the max distance and the lat/lng of the user
$q=$pdo->query("SELECT * from applicants where ApplicantID = ".$user);
// $statement =$pdo->prepare($getdata);
$f = $q->fetch();
$baselat=$f['Lat'];
$baselng=$f['Lng'];
$radius=$f['Distance'];
// inseert from select
// select gets all agents within the $radius miles of the applicants preferred location
// and inserts it into a table with their id, the propery id and the distance
$sql="insert into applicantportfolio (ID,ApplicantID, DistanceFrom)
select ID, ".$user." as ApplicantID, sqrt(power(69.1*(lat - ".$baselat."),2)+power(69.1*(lng - ".$baselng.")*cos(lat/57.3),2)) as DistanceFrom from agents where sqrt(power(69.1*(lat - ".$baselat."),2)+power(69.1*(lng - ".$baselng.")*cos(lat/57.3),2)) <= ".$radius;
$statement =$pdo->prepare($sql);
$statement->execute();
?>
Will can look into converting into a custom query but that probably wont be until the weekend
P.S. Formula returns Miles, will need to be converted to Km