Hi friends,
We creating our first BuildApp project, consisting of a warehouse app. In this project, we have a requirement, in which we need to validate for the following conditions.
1) We need to add a geolocation point for 10 km radius. If the admin is outside the 10 km radius of the warehouse, then an error message needs to be triggered.
2) In the second scenario, we need to trigger an error message in case, the admin is inside 10 km radius of the warehouse.
We have currently fetched the present GPS details using Longitude and Latitude, by using Sensor variables.
In Javascript tool, in the market place, we have added the required logic, but the answer is not coming.
The JSP coding is as follows.
<script>
$(document).ready(function() {
$(‘#getLocation’).click(function() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(success, error);
}
else {
$(‘#result’).text(“Geolocation is not supported by this browser.”);
}
});
function success(position) {
const latitude = position.coords.latitude;
const longitude = position.coords.longitude;
const geofence = {
lat: 37.7749,
lng: -122.4194,
radius: 10
};
const distance = calculateDistance(latitude, longitude, geofence.lat, geofence.lng);
if (distance <= geofence.radius) {
$(‘#result’).text(“You are within the geofenced area.”);
} else {
$(‘#result’).text(“You are outside the geofenced area.”);
}
}
function error() {
$(‘#result’).text(“Unable to retrieve your location.”);
}
function calculateDistance(lat1, lon1, lat2, lon2) {
const R = 6371e3;
const φ1 = toRadians(lat1);
const φ2 = toRadians(lat2);
const Δφ = toRadians(lat2 – lat1);
const Δλ = toRadians(lon2 – lon1);
const a = Math.sin(Δφ / 2) * Math.sin(Δφ / 2) +
Math.cos(φ1) * Math.cos(φ2) *
Math.sin(Δλ / 2) *
Math.sin(Δλ / 2);
const c = 2 * Math.atan2(Math.sqrt(a),
Math.sqrt(1 – a));
return
R * c;
}
function toRadians(degrees) {
return
degrees * Math.PI / 180;
}
});
</script>
We are unable to get validation on the required fields. Kindly provide your inputs, since we are new to SAP BuildApps.
Regards,
Mehak