URGENT !!! Using geopoint in database to find out which geopoint is the closest to the current location
I have read your guide
https://devcenter.appery.io/tutorials...
However it has SCSX014 script can't be executed error.
I am trying to find out which geopoint is the closest to the current location
My database is called Mashreq and I have a geopoint called Geopoint, arranged by the column called - Branch Name
The one with bold is the thing that I changed.
Or Maybe you can suggest any other ways to get the nearest location with geopoint from current location?
var dbId = "562508ade4b0335a64abc1b9/collections/5628ff2ae4b0335a64b704db"; //Change this value to your database id
var responseBody = {};
// Get request parameters
var latitude = request.object().latitude;
var longitude = request.object().longitude;
var radius = request.object().radius;
// The function that calculates the distance between two points
function getDistance(lat1, lat2, lon1, lon2) {
Code: Select all
var R = 3959; // Earth radius in miles
var dLat = (lat2 - lat1) * Math.PI / 180;
var dLon = (lon2 - lon1) * Math.PI / 180;
var lat1 = lat1 * Math.PI / 180;
var lat2 = lat2 * Math.PI / 180;
var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.sin(dLon / 2) * Math.sin(dLon / 2) * Math.cos(lat1) * Math.cos(lat2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
var d = R * c;
return d;
}
try {
// Check if all the necessary parameters are present and valid
if (latitude && longitude && radius && !isNaN(latitude) && !isNaN(longitude) && !isNaN(radius)) {
Code: Select all
var results = [];
var params = {};
// Sort the results by contact name
params.sort = [b] "Branch_Name"; [/b]
// Get all customers from the database
[b] var geopoint = Collection.query(dbId, "Geopoint", params);[/b]
[b] for (var i = 0; i < geopoint.length; i++)[/b] {
// Geocode customer's address with Google API
var XHRResponse = XHR.send("GET", "[url=https://maps.googleapis.com/maps/api/geocode/json]https://maps.googleapis.com/maps/api/...[/url]", {
"parameters": {
[b] "Geopoint": Mashreq[i].Geopoint,[/b]
"sensor": "false"
}
});
console.log(XHRResponse);
if (XHRResponse.status == 200) {
// Get coordinates from the response
var lat = JSON.parse(XHRResponse.body).results[0].geometry.location.lat;
var lng = JSON.parse(XHRResponse.body).results[0].geometry.location.lng;
// If the customer's coordinates are within the radius,
// add the customer record to the results array
if (getDistance(lat, latitude, lng, longitude) [b] results.push(geopoint[i]);[/b]
}
}
}
responseBody["results"] = results;
response.success(responseBody, "application/json");
} else {
response.error("Wrong parameters", 400);
}
} catch (e) {
console.log(e);
var response = JSON.parse(jqXHR.responseText);
response.error(response.description, 400);
}