UH
Posts: 0
Joined: Wed Aug 06, 2014 5:39 pm

Creating server script

Hi,

Was hoping if anyone could help me with some direction.

I am following this tutorial: http://devcenter.appery.io/tutorials/...

I have created a database, UI, and Geolocation service

My question is, if I am using coordinates in the database instead of addresses, how would I modify the server script as it uses Google Maps geocoding API to convert addresses into coordinates (in the tutorial).

Thanks!

Evgene Karachevtsev
Posts: 12
Joined: Mon Apr 28, 2014 1:12 pm

Creating server script

Hello,

In this case, you do not need to perform the request of coordinates at:
prevar XHRResponse = XHR.send("GET", "https://maps.googleapis.com/maps/api/geocode/json", {
"parameters": {
address: customers.address,
"sensor": "false"
}
});/pre
You can immediately get these coordinates, and use them in your code

UH
Posts: 0
Joined: Wed Aug 06, 2014 5:39 pm

Creating server script

Thanks. That's what I was thinking. So just take out that part and keep the rest as it is?

obullei
Posts: 0
Joined: Thu Jun 05, 2014 12:17 am

Creating server script

Hello!

You are right.

UH
Posts: 0
Joined: Wed Aug 06, 2014 5:39 pm

Creating server script

Hi

I have done that. But when I test the service, I am getting a test fail with this response

{
"status":"400 Bad Request",
"url":"https://api.appery.io/rest/1/code/904...",
"response":"An error occured"
}

obullei
Posts: 0
Joined: Thu Jun 05, 2014 12:17 am

Creating server script

Hello!

Please check if there are any errors on the tab "trace".
Please send us some screenshots.

UH
Posts: 0
Joined: Wed Aug 06, 2014 5:39 pm

Creating server script

Hi Arina,
The trace said XHRResponse was not defined. Now when I test it, i get this:

Image

It is not displaying the parameters: companyname, phone number, latitude, longitude.

This my script:
var dbId = "53dxxxxxxxxxxxxxxx....(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) {

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 = {}; 

 var XHRResponse = {}; 

 // Sort the results by company name 

 params.sort = "companyName"; 

 // Get all customers from the database 

 var customers = Collection.query(dbId, "customers", params); 

 for (var i = 0; i < customers.length; i++) { 

   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) [url=https://d2r1vs3d9006ap.cloudfront.net/s3_images/1085019/database.jpg?1407421625][img]https://d2r1vs3d9006ap.cloudfront.net/s3_images/1085019/database_inline.jpg?1407421625[/img] [/url]
UH
Posts: 0
Joined: Wed Aug 06, 2014 5:39 pm

Creating server script

It didn't post the rest of my code. It is from the tutorial listed in my first post.

I have attached a screenshot of my database collection, customers.

Thanks!

Yurii Orishchuk
Posts: 0
Joined: Fri Feb 14, 2014 8:20 am

Creating server script

Hello,

Please take a look on the screen shot to understand how you can to debug your server script:

http://prntscr.com/4ampqi/direct

Also in the "trace" tab you can see console.log from the the script. So you can logout in your script things you need.

Also please share your server script with a href="mailto:support@appery.io" rel="nofollow"support@appery.io/a and tell us it name.

Regards.

Return to “Issues”