Eric Leroyer
Posts: 0
Joined: Mon Sep 08, 2014 1:45 pm

Return Value to Label if any coordinates are within Radius

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.

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

Return Value to Label if any coordinates are within Radius

Hello Eric,

Your error
preError: 9&#46;10&#46;2014 12:53:38 pm: Script Geocoding: SyntaxError: Missing catch or finally after try ( @ 80 : 0 ) - } else {/pre
means that you forgot to specify a catch or finally after try. Please check the syntax of this construct(statement) https://developer.mozilla.org/en-US/d...
You either don't specify it all or there is some issue with curly brackets {}, so your catch or finally was in the wrong place.

Eric Leroyer
Posts: 0
Joined: Mon Sep 08, 2014 1:45 pm

Return Value to Label if any coordinates are within Radius

I have I thought it was all in the right place see image? Image

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

Return Value to Label if any coordinates are within Radius

Eric,

Remove the curly brace on line 74.

Return to “Issues”