Mark Wong
Posts: 0
Joined: Sat Feb 14, 2015 7:47 am

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

}

Mark Wong
Posts: 0
Joined: Sat Feb 14, 2015 7:47 am

URGENT !!! Using geopoint in database to find out which geopoint is the closest to the current location

I am trying another alternative method of listing all the geopoint out first and calculate the distance between at the JS of mapping repsonse. The code is when the distance calculated is smaller than the one stored in the variable, then it will replace it and compare to the next one and so on

But value.Geopoint is not working due to some reason.

code
var lat1 = sessionStorage&#46;getItem("Latitude");
var lon1 = sessionStorage&#46;getItem("Longitude");
var lat2 = value&#46;Geopoint[0];
var lon2 = value&#46;Geopoint[1];
&#47;&#47; The function that calculates the distance between two pointsjavascript: void(0);
function getDistance(lat1, lat2, lon1, lon2) {

Code: Select all

 var R = 3959; &#47;&#47; Earth radius in miles 
 var dLat = (lat2 - lat1) * Math&#46;PI / 180; 
 var dLon = (lon2 - lon1) * Math&#46;PI / 180; 
  lat1 = lat1 * Math&#46;PI / 180; 
  lat2 = lat2 * Math&#46;PI / 180; 
 var a = Math&#46;sin(dLat / 2) * Math&#46;sin(dLat / 2) + Math&#46;sin(dLon / 2) * Math&#46;sin(dLon / 2) * Math&#46;cos(lat1) * Math&#46;cos(lat2); 
 var c = 2 * Math&#46;atan2(Math&#46;sqrt(a), Math&#46;sqrt(1 - a)); 
 var d = R * c; 

 if (sessionStorage&#46;getItem("Nearest") > d){ 
     sessionStorage&#46;setItem("Nearest", d); 
 } 

}
/code
Image

Maybe you can choose which one is more possible?

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

URGENT !!! Using geopoint in database to find out which geopoint is the closest to the current location

Hello Mark,

Unfortunately custom app logic is outside the scope of our support, but perhaps this post may be useful for you:
https://getsatisfaction.com/apperyio/...

Mark Wong
Posts: 0
Joined: Sat Feb 14, 2015 7:47 am

URGENT !!! Using geopoint in database to find out which geopoint is the closest to the current location

Hello Evgene,

I am using the second method, and I tried to separate the geopoint to two separate string in the database, : Lat & Long

Apparently before I can use value.someVariable in the JS of response mapping to get other response value, however its not working here, any idea?

There is no error

Mark Wong
Posts: 0
Joined: Sat Feb 14, 2015 7:47 am

URGENT !!! Using geopoint in database to find out which geopoint is the closest to the current location

I think I resolve my problem using the second method, Thank you

EasyFab
Posts: 0
Joined: Sun Nov 15, 2015 4:06 am

URGENT !!! Using geopoint in database to find out which geopoint is the closest to the current location

Hello
Can you tell us how you resolved your problem exactly ?
Thanks

Mark Wong
Posts: 0
Joined: Sat Feb 14, 2015 7:47 am

URGENT !!! Using geopoint in database to find out which geopoint is the closest to the current location

Hi EasyFab,

I map the $ to a grid with following code:

code
var lat1 = sessionStorage&#46;getItem("Latitude");
var lon1 = sessionStorage&#46;getItem("Longitude");
var lon2 = value&#46;Geopoint[0];
var lat2 = value&#46;Geopoint[1];
&#47;&#47; as the Geopoint of Appery Database is an array

&#47;&#47; The function that calculates the distance between two points
var R = 3959; &#47;&#47; Earth radius in miles
var dLat = (lat1 - lat2) * Math&#46;PI / 180;
var dLon = (lon1 - lon2) * Math&#46;PI / 180;
lat1 = lat1 * Math&#46;PI / 180;
lat2 = lat2 * Math&#46;PI / 180;
var a = Math&#46;sin(dLat / 2) * Math&#46;sin(dLat / 2) + Math&#46;sin(dLon / 2) * Math&#46;sin(dLon / 2) * Math&#46;cos(lat1) * Math&#46;cos(lat2);
var c = 2 * Math&#46;atan2(Math&#46;sqrt(a), Math&#46;sqrt(1 - a));
var d = R * c;

Code: Select all

 if (sessionStorage&#46;getItem("Nearest") > d){ 
     sessionStorage&#46;setItem("Nearest", d); 
     sessionStorage&#46;setItem("Branch_Name", value&#46;Branch_Name); 
     sessionStorage&#46;setItem("Destination", value&#46;Geopoint[1]+","+value&#46;Geopoint[0]); 
     $("[name=mobilelabel_11]")&#46;text(value&#46;Branch_Name);  
 } 

&#47;&#47; replace the nearest value and name when the distance between is shorter than d

/code

Return to “Issues”