Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Distance Between Two Cities
Message
General information
Forum:
Visual FoxPro
Category:
Client/server
Miscellaneous
Thread ID:
01149128
Message ID:
01151401
Views:
29
Forget paying for what you can code yourself. I have this little piece of code that calculates distance between two cities. It requires a table called "Geo" which lists all zip codes by lat and long.

It is defined as:
create table 'geo' ;
   (zip c(5), ;
   city c(28), ;
   state c(2), ;
   county c(22), ;
   lat n(7,4), ;
   long n(8,4))
mkcur8a.PRG
* this formula code thanks to Chris.Michels@nau.edu
* coded into Visual Foxpro by artgr@aol.com
* If you are looking for a way to determine your latitude and longitude go to
* http://jan.ucc.nau.edu/~cvm/latlon_find_location.html

LOCAL lat, long, r, a1, b1, a2, b2

CLEA
* $r = radius of the earth in whatever units you want
r=3963.1 &&statute miles

* $a1 = lat1 in radians
* $b1 = lon1 in radians
SELECT lat, LONG INTO CURSOR "query" FROM "geo" WHERE city == "Daytona Beach"
SCATTER MEMVAR
a1=DTOR(m.lat)
b1=DTOR(m.long)

* $a2 = lat2 in radians
* $b2 = lon2 in radians
SELECT lat, LONG INTO CURSOR "query" FROM "geo" WHERE city == "Ocala"
SCATTER MEMVAR
a2=DTOR(m.lat)
b2=DTOR(m.long)

? 'Distance in miles from Ocala to Daytona Beach '+CHR(13)+CHR(10)+'is approx '
?? ACOS(COS(a1)*COS(b1)*COS(a2)*COS(b2) + COS(a1)*SIN(b1)*COS(a2)*SIN(b2) + SIN(a1)*SIN(a2)) * r

SELECT AVG(lat) AS lat, AVG(LONG) AS LONG ;
   INTO CURSOR "query" ;
   FROM geo ;
   WHERE state = "FL" AND city = "Gainesville"
SCATTER MEMVAR
a1=DTOR(m.lat)
b1=DTOR(m.long)
? 'Distance in miles from Ocala to Gainesville '+CHR(13)+CHR(10)+'is approx '
?? ACOS(COS(a1)*COS(b1)*COS(a2)*COS(b2) + COS(a1)*SIN(b1)*COS(a2)*SIN(b2) + SIN(a1)*SIN(a2)) * r

USE IN "Geo"
USE IN "query"
73,
Art
N4HZK
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform