Win a copy of Java EE 8 High Performance this week in the Java/Jakarta EE forum!
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Calculating distance between two points represented by lat,long upto 15 feet accuracy

Ranch Hand
Posts: 541
I have converted the formula into Java provided here. But accuracy is a problem. We are using GPS coordinates.

Is GPS coordinate good enough input for feet level accuracy? Or I should be looking into something else?

Saurabh Pillai
Ranch Hand
Posts: 541
Problem is I am not getting accuracy. The distance that I get using the formula mentioned in above link, is not same as actual distance.

For example, actual 25 feet distance we are getting 7 feet distance. Application could be I want to open door when I am 10 feet away from my door. I am pushing my location to server every second. and server has location of my door. Both in GPS coordinates.

Saurabh Pillai
Ranch Hand
Posts: 541

And then I use Math.round(distance); to convert it to long.

author
Sheriff
Posts: 23364
127

Saurabh Pillai wrote:I have converted the formula into Java provided here. But accuracy is a problem. We are using GPS coordinates.

Is GPS coordinate good enough input for feet level accuracy? Or I should be looking into something else?

I believe that most GPS are accurate to about 4 meters -- and even that number is kinda rough.

Henry

Saurabh Pillai
Ranch Hand
Posts: 541
Do you have any alternative way that I can look for? My target is to find the distance automatically and trigger the event when threshold is reached.

Sheriff
Posts: 23273
46
So is your problem that your GPS coordinates aren't accurate enough, or that your formula is incorrect?

(I find your formula hard to understand with all those Math.toRadians cluttering it up, and I haven't tried to derive it myself from trigonometry, but it looks to me like you're approximating the earth as a sphere, which is not entirely accurate. However I have no idea how much error that would introduce.)

Saurabh Pillai
Ranch Hand
Posts: 541

Paul Clapham wrote:So is your problem that your GPS coordinates aren't accurate enough, or that your formula is incorrect?

I don't have any way to isolate the problem. I have been given the requirement calculating the distance from GPS coordinates. I googled it and came across the link provided in my first post and assumed it is correct.
Do you have any other better way to calculate the distance?

Paul Clapham
Sheriff
Posts: 23273
46
I don't know about "better" -- but here's the formula I've used in the past to calculate distances from my GPS tracks:

And it's worked pretty well for me. (And yes, I did get it from the internet, but don't ask me where because I've forgotten.)

lowercase baba
Bartender
Posts: 12601
50
What exactly do your specs say? I found this:

"the GPS signal in space will provide a "worst case" pseudorange accuracy of 7.8 meters at a 95% confidence level." It goes on to say that real-world data indicates a good GPS receiver can frequently be accurate to within 3 meters. If you have a base station, you can be accurate to within a few centimeters.

Bartender
Posts: 10575
66

Saurabh Pillai wrote:And then I use Math.round(distance); to convert it to long.

1. You could also save yourself a lot of method calls by simple converting this.latitiude and this.longditude to radians once.

2. I seems to me that you could also encapsulate latitude and longditude into a Position class, and then implement a
public double distance(Position from) { ...
method. Seems much less hassle to me than trying to deal with individual axes. You could then also convert your latitude/longditude values to radians at creation time.

I've broken yours up this time, but for future reference, please remember:
80 characters max.
(the SSCCE page actually recommends 62)
And that includes string literals AND comments.

Oh, and you're usually better off using spaces for indentation, rather than TABs.

Thanks.

Winston

Saurabh Pillai
Ranch Hand
Posts: 541
@Paul Clapham Thank you I will look into it.

@fred rosenberger We are using iPhone GPS. But after some of the discussion here and reading up material, it feels like I am not using right tool for the problem.

@Winston Gutkowski
1. Agree.
2. Why do I need a separate class for this? I am trying to use it more as an utility method.

Winston Gutkowski
Bartender
Posts: 10575
66

Saurabh Pillai wrote:2. Why do I need a separate class for this? I am trying to use it more as an utility method.

Because Java is an Object-Oriented language, and a Position is a lot more than just two double values stuck together. It describes a point on the surface of a sphere (in fact, a very specific sphere: the Earth).

If you want to make a utility out of it, just create a:in your Position class, that returns p1.distance(p2).

And thinking about it, distanceTo(Position) might be a better name for your instance method.

Winston

Ranch Hand
Posts: 334
2
It seems to me there are a few issues at play here

First is the inherent resolution of GPS with the equipment you have. This depends on a lot of things from the number of satellites being seen and used at the moment and the precision that the receiver sends to your program. If you are trying to use GPS indoors the number of satellites is very limited and a mobile phone may be using cell towers to report your position.

Next I've played with some cheap USB and some handheld aviation grade GPS that use the NMEA protocol and end up sending degrees to 4 or 5 decimal places. That's a precision of about 10 or 1 meters (circumference of the earth/360 degrees/10^4 or 5)

Finally there's distance calculating algorithm.

You can test your algorithm by fixing longitude or latitude and testing small changes in the other then both. Paul's algorithm estimates the radius of the earth at 6371 km, I don't see the constants you're using.

To test the phone's accuracy I would go outside to make sure you're receiving a good gps signal and see what it reports for locations you should be able to see how far you have to move to get a different reading and how consistent it is a fixed locations.

My guess is that if you need accuracy to a foot or 2 indoors, an iPhone is not going to work for you.

Joe

Marshal
Posts: 57453
175
There is another problem if you are using that GPs in real life: the Earth is not spherical. The simple trigonometrical method assume the Earth is a sphere.

Joe Areeda
Ranch Hand
Posts: 334
2

Campbell Ritchie wrote:There is another problem if you are using that GPs in real life: the Earth is not spherical. The simple trigonometrical method assume the Earth is a sphere.

That's true but when calculating distances on the order of a few feet or a few thousand feet it probably won't be a significant source of error.

Paul Clapham
Sheriff
Posts: 23273
46
In my case the two points I've been calculating the distance between are two points which are no more than one minute of walking distance apart.

Campbell Ritchie
Marshal
Posts: 57453
175
Agree: if the two points are that close together my errors will cancel each other out.

 It is sorta covered in the JavaRanch Style Guide.