Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Distance Calculation in Foxpro using Lat / Long 1

Status
Not open for further replies.

ceoautofusion

Programmer
Jun 3, 2012
4
US
thread184-1621436

Here is the working foxpro code...

lnlat1 = 32.61
lnlon1 = -117.07
lnlat2 = 33.12
lnlon2 = -117.09

lnR = 3958.7558657440545 && Radius of earth in Miles
lnLat = DTOR(lnLat2 - lnLat1)
lnLon = DTOR(lnLon2-lnLon1)
lnA = SIN(lnLat / 2) * SIN(lnLat / 2) + COS(DTOR(lnLat1)) * ;
COS(DTOR(lnLat2)) * SIN(lnLon / 2) * SIN(lnLon / 2)
lnC = 2 * ATN2(SQRT(lnA), SQRT(1 - lnA))
lnD = lnR * lnC && distance between the points in km.
? lnD
RETURN


Here is the equivalent Javascript Code
lat1 = 32.61;
lon1 = -117.07;
lat2 = 33.12;
lon2 = -117.09;

//Radius of the earth in: 1.609344 miles, 6371 km | var R = (6371 / 1.609344);
var R = 3958.7558657440545; // Radius of earth in Miles
var dLat = toRad(lat2-lat1);
var dLon = toRad(lon2-lon1);
var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(toRad(lat1)) * Math.cos(toRad(lat2)) *
Math.sin(dLon/2) * Math.sin(dLon/2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
var d = R * c;
document.write(d);

function toRad(Value) {
/** Converts numeric degrees to radians */
return Value * Math.PI / 180;
}

Distance should be 35.256706166892230
 
This topic has been discussed a few times already.

Do a Search within this forum area for longitude and you will find other postings on the topic.

Good Luck,
JRB-Bldr

 
When I searched on Google for 'Foxpro distance calculation' the thread referenced at the top showed up first. If you read the thread it was left in an incomplete status and I couldn't updated it - as it had been closed. So the best I could do was to create a new trhead and reference the old one.

 
You didn't yet tell us the problem. You say the foxpro code is working. Does that mean the problem is with the javascript translation of the code?

Actually, if I wrap it into <script> tags, the javascript yields the same result.

If that doesn't answer it, you may look into the latest discussion on that topic of determining the distance from May this year, which was quite fruitful: thread184-1683179.

Bye, Olaf.
 
My example was meant to be or more complete version of the original thread.
In the original thread, the poster said that he did not test the code.
I am showing javascript code that I found on the web - enhanced from what the original poster had obviously used, and the corresponding Foxpro Code - when run, both get the same result - thus I have tested the Foxpro code against the Javascript code.

 
When I searched on Google for 'Foxpro distance calculation' ...

I did not say to a GOOGLE Search.

I said to a Search within this forum using the Search tool at the top of this post (it appears like a TAB)

As has been asked above, do you have a Problem with your code?
Or what?

Good Luck,
JRB-Bldr

 
Now if you search Google - this is the thread that shows up first - it has replaced the old thread.
So mission accomplished - if someone needs to know how to do a distance calculations in Foxpro, this thread will show them how. That is what I was looking for when I searched - I found something incomplete, I updated this site, and now the next person will have a complete example.

--Roy
 
Roy,

It's good that you posted a working solution. It's likely to be of benefit to others. The misunderstanding was simply caused by the fact that we thought you were asking a question about the code. Please don't be put off by this.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips, training, consultancy
 
I can only second Mike,

no offence was taken by us, but we looked for a question in your post, because you posted a question thread type. This forum has a mechanism to post such tips as tips, just click on thread type tip.

Next time.

Bye, Olaf.
 
If your solution is a 'complete' solution, you might want to consider posting your code as an FAQ of this forum area so that it can be found in that manner as well.

Good Luck,
JRB-Bldr
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top