Hi
I'm in the process of trying to run a script very similar to the article below, the different is that I have the longitude and latitude coordinates already stored in separate column where as the article below has the address which is required to be converted before check if within a radius.
Please find my error below and the code:
Error:
9.10.2014 12:53:38 pm: Script Geocoding: SyntaxError: Missing catch or finally after try ( @ 80 : 0 ) - } else {
Script Code:
// Database Id
var dbId = locations; //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) {
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)) {
var results = [];
var params = {};
// Sort the results by contact name
params.sort = "_id";
// Get all customers from the database
var customers = Collection.query(dbId, "location", params);
for (var i = 0; i < customers.length; i++) {
// Get coordinates from the response
var lat = data.Lat;
var lng = data.Lng;
// If the customer's coordinates are within the radius,
// add the customer record to the results array
if (getDistance(lat, latitude, lng, longitude) ve spent many hours trying to figure out what I am doing wrong.