The calculateDistance() method calculates the distance between two sets of geographical coordinates (endpoints). We are trying to determine which set of coordinates a user is closest to:

public static double calculateDistance(double lat1, double lng1,double lat2, double lng2) {int r = 6371; // average radius of the earth in kmdouble dLat = Math.toRadians(lat2 - lat1);double dLon = Math.toRadians(lng2 - lng1);double a = Math.sin(dLat / 2) * Math.sin(dLat / 2)+ Math.cos(Math.toRadians(lat1))* Math.cos(Math.toRadians(lat2)) * Math.sin(dLon / 2)* Math.sin(dLon / 2);double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));double d = r * c;double e = 0.621371 * d;return e;}

How do you determine whether a user in a certain radius of a location (represented by a set of coordinates in latitude and longitude)?

Is there any way to query if a user is geographically located between a range of coordinates (ie between 15.0 and 20.0 degrees latitude and 5.0 and 20.0 degrees longitude)?

create function calculateDistance(lat1 real, lng1 real, lat2 real, lng2 real ) returns real as
$$
declare
dLat real := (lat2 - lat1)*pi()/180.0;
dLon real := (lng2 - lng1)*pi()/180.0;
a real;

Have you considered using postgis which uses SQL query && operator to reduce data slice before you do the compute intensive ST_Distance function.

SELECT the_geom
FROM geom_table
WHERE the_geom && 'BOX3D(90900 190900, 100100 200100)'::box3d
AND
ST_Distance(the_geom, ST_GeomFromText('POINT(100000 200000)', -1)) < 100;

I don't understand your schema very well the only table that has latitude is table location but no other table references it.

select * from location as loc where loc.lat between 15.0 and 20.0 and
loc."long" between 5.0 and 20.0;

select calculateDistance( loc.lat, loc."long", 18.4,7.6 ) as distance, loc.* from location as loc where calculateDistance( loc.lat, loc."long", 18.4,7.6 )
< 5.0;

select calculateDistance( loc.lat, loc."long", 18.4,7.6 ) as distance, loc.* from
(select * from location where location.lat between 15.0 and 20.0 and
location."long" between 5.0 and 20.0 ) as loc
where calculateDistance( loc.lat, loc."long", 18.4,7.6 ) < 5.0;

Thank you for the tip. Unfortunately, there were errors in your SQL command. loc is not a table. loc should be an instance of the location table. Also, I cannot retrieve the latitude and longitude using the dot operator, as you presented in your script.

Do I need to make getters and setters for the lat and long in my code or is there a way to retrieve those values through SQL?

Also, the function parameters seem to be incorrect. It should be passing four numeric values (decimals), rather than two real and two numeric values. It also says 'missing FROM-clause entry for table', even though it appears that you have it.

Could you look at these, and get back to me?

Thanks.

0

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

create function calculateDistance(lat1 real, lng1 real, lat2 real, lng2 real ) returns real as
$$
declare
dLat real := (lat2 - lat1)*pi()/180.0;
dLon real := (lng2 - lng1)*pi()/180.0;
a real;
begin
a := sin( dLat / 2 ) ^ 2 + cos( lat1 * pi()/180.0) * cos( lat2 * pi()/180.0) * sin( dLon / 2 ) ^ 2;
return 6371.0 * 0.621371 * 2.0 * atan2(sqrt(a), sqrt(1.0 - a));
end;
$$ language plpgsql;

select calculateDistance( loc.lat, loc."long", 18.4,7.6 ) as distance, loc.* from
(select * from location where location.lat between 15.0 and 20.0 and
location."long" between 5.0 and 20.0 ) as loc
where calculateDistance( loc.lat, loc."long", 18.4,7.6 ) < 125.0;

Thank you for your help. It worked in PostGreSQL. I noticed that your function had hard-coded values (for testing, of course). Is there a way to write this query with general parameters in Java? (ie loc1.lat, loc2.long)? I have been trying to write this query in my Java source code but have experienced compiler errors.

Are you escaping the double quotes with the backslash -- difficult to know what the issue is without further detail. The java should look something like ..

"select calculateDistance( loc.lat, loc.\"long\", ?,? ) as distance, loc.* from
(select * from location where location.lat between ? and ? and
location.\"long\" between ? and ? ) as loc
where calculateDistance( loc.lat, loc.\"long\", ?,? ) < ?;"

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Introduction
This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.

Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it.
Define Error Message:
Line Numbers:
Type of Error:
Break Down…

Viewers will learn about the regular for loop in Java and how to use it.
Definition:
Break the for loop down into 3 parts:
Syntax when using for loops:
Example using a for loop: