Lee Fee
Posts: 0
Joined: Sun Mar 29, 2015 11:13 pm

Geocoding tutorial - modifying server code if longitude and latitude values are known and stored in a database

Hi, I am attempting to modify the server code found in the geocoding tutorial:
https://devcenter.appery.io/tutorials...

The only difference is that longitude and lattitude values of all 'customers' are stored on a database.

Having explored other solutions to similar problems, I wish to avoid:
Mapping 'Where' queries with JavaScript (using $nearSphere)
Database services (in preference of server code services)

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

Main question:
From the geocoding tutorial, would it be possible to remove the server code lines 55 to 77 and replace it with code that queries a database for known values of lat and lng?

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

// server code lines 55 to 77 from tutorial shown below:

// Geocode customer's address with Google API
var XHRResponse = XHR.send("GET", "https://maps.googleapis.com/maps/api/...", {
"parameters": {
"address": customers.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;

I am not sure of the right approach but was thinking something along the lines of:
var lat = Collection.query(dbId, "customers");
var long = Collection.query(dbId, "customers");

However, the Response is not populated with the information I expect to receive, i.e:
Status:200
{"results":[]}

Something is incorrect with the code above. However, the results are properly populated when abitary values are set for the customer location, i.e:
var latitude = '50';
var longitude = '0';

Any assistance you can provide would be much appreciated.

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

Geocoding tutorial - modifying server code if longitude and latitude values are known and stored in a database

Hi Lee,

Yes you can replace these lines of code with query of db:

pre

var params = {}; //Define parameters object
//Query criteria: here you can set needed search criteria
params.criteria = {
};
var result = Collection.query(DB_id, "collectionName", params);

//Check trace to see actual response
console.log(result);

if(result && result[0]){
var item = result[0];
var lat = item.lat;
var lng = item.lng;
}

/pre

Regards.

Lee Fee
Posts: 0
Joined: Sun Mar 29, 2015 11:13 pm

Geocoding tutorial - modifying server code if longitude and latitude values are known and stored in a database

Thanks Yurii.

In the app builder, the service response either dumps the entire database or displays { "results":[ ] } depending on the radius value. While this is an good improvement, it isn't quite what I was hoping for.

For some reason the response isn't being filtered correctly by the function "if (getDistance..." stated in the tutorial. I have several hours tinkering with the code to no avail. Would it be possible to send you a copy of the server code?

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

Geocoding tutorial - modifying server code if longitude and latitude values are known and stored in a database

Hi Lee,

Yes you can share this code here. Please use <code</code tag to wrap you code correctly.

Also please describe what exactly query/search not working.

Regards.

RobertJay
Posts: 0
Joined: Fri Jun 15, 2012 1:32 pm

Geocoding tutorial - modifying server code if longitude and latitude values are known and stored in a database

Yurii, can you please share the solution on this-- I have the same exact issue as Lee. Thanks.

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

Geocoding tutorial - modifying server code if longitude and latitude values are known and stored in a database

Hi Robert,

Unfortunatly we don't have a solution. It's Lees application.

May be Lee could help.

Regards.

RobertJay
Posts: 0
Joined: Fri Jun 15, 2012 1:32 pm

Geocoding tutorial - modifying server code if longitude and latitude values are known and stored in a database

Yurii - the application is not Lee's - it's from YOUR tutorial. And in the tutorial you clearly suggest that developers store geolocation information in the database for anything but tiny queries. And now you say you have no solution to making that work! Very disappointing support. You could find a solution if you wanted to with a few lines of code.

Lee Fee
Posts: 0
Joined: Sun Mar 29, 2015 11:13 pm

Geocoding tutorial - modifying server code if longitude and latitude values are known and stored in a database

Hi Yurii, sorry about the delay. I have been busy working on other functionalites and had somewhat given up on geocoding for the time being.

Although I perhaps share a different sentiment to Robert, I do feel it would be beneficial to the community to have a example server script for this functionality.

Maybe it'd be worth considering if often requested...

RobertJay
Posts: 0
Joined: Fri Jun 15, 2012 1:32 pm

Geocoding tutorial - modifying server code if longitude and latitude values are known and stored in a database

Yurii and Lee - good news. I think I've got it working. Let me finish testing it tomorrow. If all goes well I'll post the code. Yurii - I think, if you want, updating the tutorial will be real easy. Talk to you tomorrow.

RobertJay
Posts: 0
Joined: Fri Jun 15, 2012 1:32 pm

Geocoding tutorial - modifying server code if longitude and latitude values are known and stored in a database

UPDATE: For my needs, turned out to be easy peasy: http://screencast.com/t/r5WEZUL5
Hope this helps someone else.

Return to “Issues”