Page 1 of 1

Geolocation app distance

Posted: Mon Feb 01, 2016 4:16 am
by S Butler

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);
}


Geolocation app distance

Posted: Mon Feb 01, 2016 7:34 am
by Serhii Kulibaba

Hello,

Couild you check trace information? Are there any errors?


Geolocation app distance

Posted: Mon Feb 01, 2016 1:28 pm
by S Butler

HI,
The code works fine, so there are no errors. My question was how can I return the getDistance value that is calculated in the above code as a response parameter. The code goes and pulls all of the records from my Locations table and calculates the distance from the device's current location. If the distance is less than the radius specified by the user than the system will add that record to a response array which will be returned to the app once all records are calculated. The distance is calculated with the getDistance function in the above code. How can i save the result of this calculation as parameter for each array item to be sent in the response? And then how can i sort the response parameters by this distance parameter?


Geolocation app distance

Posted: Tue Feb 02, 2016 1:30 pm
by Serhii Kulibaba

Could you use loop for each element of that response? E.g. with calling function "getDistance"