Geolocation app distance
Hi,
I have successfully followed the tutorial for the geocoding app located here: https://devcenter.appery.io/tutorials...
Everything works great, however, I need to also send the distance that is calculated for each location back as a response parameter. And then sort the locations by this distance from smallest to largest. Below is the code that I have working. Can you please help me accomplish this? Thanks!
var responseBody = {},
requestParams = {},
paramKeys = Apperyio.request.keys();
for (var key = 0; key < paramKeys.length; key++) {
requestParams[paramKeys[key]] = Apperyio.request.get(paramKeys[key]);
}
responseBody.requestBody = Apperyio.request.body();
responseBody.requestParams = requestParams;
console.log(responseBody);
Apperyio.response.success(responseBody, "application/json");
// Database Id
var dbId = "56ab983be4b0e9b36b46baba"; //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;
var locationSearchName = request.object().name;
var locationSearchService = request.object().service;
// 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 location name
params.sort = "name";
// Get all locations from the database
var locations = Collection.query(dbId, "Locations", params);
for (var i = 0; i < locations.length; i++) {
// 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": {
"address": locations[i].address,
"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;
var locationName = locations[i].name.toLowerCase();
var locationService = locations[i].servicesOffered.toLowerCase();
// If the customer's coordinates are within the radius,
// add the customer record to the results array
if (getDistance(lat, latitude, lng, longitude) -1))) {
results.push(locations[i]);
}
}
}
responseBody["results"] = results;
response.success(responseBody, "application/json");
} else {
response.error("Wrong parameters", 400);
}
} catch (e) {
console.log(e);
response.error("An error occured", 400);
}