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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

GOOGLE/MAPQUEST Question 5

Status
Not open for further replies.

WIREMESH

Programmer
Mar 15, 2004
109
0
0
US
Using any type of mapping API(Google, MApquest/Microsoft, etc.) I want to able to track the distance traveled between sites.

For instance (3 SAMPLE SITES)

starting site1: 1500 Market St, Phila PA 19102
site2: 5712 Broomall st. Phila pa 19143
site3: 1213 North st. cAMDEN, NJ 11111

The results would be distance traveled from sitex to sitey to is xxx.xx miles.

How can this be accomplished using any of the web api's using VFP?
 
Try this. (This requires a Google key - it is free)

Code:
cAddress1 = "123 main st anywhere ny"
cAddress2 = "123 oak st anywhere ny"

?distance(google_geo(cAddress1),google_geo(cAddress2))

PROCEDURE google_geo
LPARAMETERS tcAddress
LOCAL CRLF, cUrl, oHTTP, cKey, cResult, cTag, lTag, oIE
  STORE CHR(13)+CHR(10) TO CRLF
  cKey = MyGoogleKey
  
  SET LIBRARY TO Home()+"Foxtools.fll" ADDITIVE
  tcAddress = CHRTRAN(ALLTRIM(reduce(tcAddress)),CHR(32), "+")

  cURL = "[URL unfurl="true"]http://maps.google.com/maps/geo?q="[/URL] + tcAddress + "&output=csv&key=" + cKey
  
  oHTTP = CreateObject("MSXML2.XMLHTTP")
  oHTTP.Open("GET", cURL, .f.)
  oHTTP.Send
  cResult=(oHTTP.ResponseText)
 
  nCode=GETWORDNUM(cResult,1,",")
  nAccuracy=GETWORDNUM(cResult,2,",")
  nLat=GETWORDNUM(cResult,3,",")
  nLon=GETWORDNUM(cResult,4,",")
    
  STRTOFILE(cResult,'cut.txt')
  RETURN nLon + "," + nLat + "," + nCode + "," + nAccuracy
 ENDIF

*!*  CODE
*!*  G_GEO_SUCCESS (200)
*!*    No errors occurred; the address was successfully parsed and its geocode has been returned. (Since 2.55)

*!*  G_GEO_SERVER_ERROR (500)
*!*    A geocoding request could not be successfully processed, yet the exact reason for the failure is not known. (Since 2.55)

*!*  G_GEO_MISSING_ADDRESS (601)
*!*    The HTTP q parameter was either missing or had no value. (Since 2.55)

*!*  G_GEO_UNKNOWN_ADDRESS (602)
*!*    No corresponding geographic location could be found for the specified address. This may be due to the fact that the address is relatively new, or it may be incorrect. (Since 2.55)

*!*  G_UNAVAILABLE_ADDRESS (603)
*!*    The geocode for the given address cannot be returned due to legal or contractual reasons. (Since 2.55)

*!*  G_GEO_BAD_KEY (610)
*!*    The given key is either invalid or does not match the domain for which it was given. (Since 2.55)

*!*  Accuracy
*!*    0 Unknown location. (Since 2.59)
*!*    1 Country level accuracy. (Since 2.59)
*!*    2 Region (state, province, prefecture, etc.) level accuracy. (Since 2.59)
*!*    3 Sub-region (county, municipality, etc.) level accuracy. (Since 2.59)
*!*    4 Town (city, village) level accuracy. (Since 2.59)
*!*    5 Post code (zip code) level accuracy. (Since 2.59)
*!*    6 Street level accuracy. (Since 2.59)
*!*    7 Intersection level accuracy. (Since 2.59)
*!*    8 Address level accuracy. (Since 2.59)

ENDPROC

PROCEDURE distance
LPARAMETERS tcGeo1, tcGeo2

  lnLon1 = VAL(GETWORDNUM(tcGeo1,1,","))
  lnLat1 = VAL(GETWORDNUM(tcGeo1,2,","))
  lnLon2 = VAL(GETWORDNUM(tcGeo2,1,","))
  lnLat2 = VAL(GETWORDNUM(tcGeo2,2,","))

  lnDistance = 3963.000000*ACOS(COS(DTOR(90-lnLat1))*COS(DTOR(90-lnLat2))+;
    SIN(DTOR(90-lnLat1))*SIN(DTOR(90-lnLat2))*COS(DTOR(lnLon1 - lnLon2)))
  
  IF lnDistance < .25
    lcDistance = TRANSFORM(ROUND(lnDistance*5280,2)) +" feet"
  ELSE
    lcDistance = TRANSFORM(ROUND(lnDistance,2)) + " miles"
  ENDIF
  
  RETURN lcDistance
ENDPROC

Mike Gagnon

If you want to get the best response to a question, please check out FAQ184-2483 first.
ReFox XI (www.mcrgsoftware.com)
 
Nice example Mike!

boyd.gif

SweetPotato Software Website
My Blog
 
Thank you.

Using VFP WithYahoo Maps Geocoding API
thread1252-1356750

Brian
 
Once code gets out of the barn there's no getting it back. [shadeshappy]
 
Actually Brian you are right, as I found this code not here but on another website (probably giving credit to soemone else), I figured it was best not to give any credit to anyone, on order not to offend anyone. But since you obviouly wrote the code, the credit goes to you.


Mike Gagnon

If you want to get the best response to a question, please check out FAQ184-2483 first.
ReFox XI (www.mcrgsoftware.com)
 
Code:
  lnDistance = 3963.000000*ACOS(COS(DTOR(90-lnLat1))*COS(DTOR(90-lnLat2))+;
    SIN(DTOR(90-lnLat1))*SIN(DTOR(90-lnLat2))*COS(DTOR(lnLon1 - lnLon2)))

Ive been searching for this formula for a while now,
Thanks Brian for finding it, and Thanks Mike for posting it again for me to trip over
wjwjr
 
Thanks for all the post. One additional question, Using the following 2 addresses the algorithm comes up with 2.76 miles. However, if I goto google maps, and get driving directions, the driving distance 3.3 miles. I recognize the driving miles are almost always going to be greater than the computed distance. How can I get the driving miles? My client wants to calculate driving miles.

cAddress1 = "1230 BURKEMONT AVE MORGANTON NC 28655"
cAddress2 = "576 E Fleming Dr, Morganton, Burke, North Carolina 28655, United States"
 
The distance calculated is the 'crow's flight' distance (not driving distance) I believe. You would need something la mapquest or mappoint to get the driving distance.

Mike Gagnon

If you want to get the best response to a question, please check out FAQ184-2483 first.
ReFox XI (www.mcrgsoftware.com)
 
the below function with return either air or drive miles

? getMiles("123 street,anycity,anystate,anyzip",("456 street,anycity,anystate,anyzip",0)
the last parameter is 0 or 1
0 for AirMiles
1 for Drive Miles

if you uncomment the NOTE goLoc1 = goMap.GetLocation(m.cLocation1)
AND goLoc2 = goMap.GetLocation(m.cLocation2)

then Comment the goLoc1 = goMap.Find(m.cLocation1)
goLoc2 = goMap.Find(m.cLocation2)

you can pass latitude,longetude as number to get same result


Code:
Function getMiles
PARAMETERS cLocation1 as string, cLocation2 as string, nAirORDrive as Number
NOTE cLocation1 AND cLocation2
IF TYPE("nAirORDrive")<>"N"
	nAirORDrive=0
ENDIF
IF nAirORDrive<0 .or. nAirORDrive>1
	nAirORDrive=0
ENDIF 

goMapApp = createobject("MapPoint.Application")
goMap = goMapApp.ActiveMap

m.myDistance1=0
m.myDistance2=0

NOTE goLoc1 = goMap.GetLocation(m.cLocation1)
NOTE goLoc2 = goMap.GetLocation(m.cLocation2)

goLoc1 = goMap.Find(m.cLocation1)
goLoc2 = goMap.Find(m.cLocation2)

gomap.Application.ActiveMap.Saved=.t.
If Type("goLoc1")="O" .and. Type("goLoc2")="O"
	myDistance1=goMap.Distance(goLoc1, goLoc2)
ELSE
	myDistance1=0
ENDIF

If Type("goLoc1")="O" .and. Type("goLoc2")="O"
	goRoute = goMap.ActiveRoute 
	goRoute.Clear()		&& Get rid of any existing waypoints.
	goRoute.Waypoints.Add(goLoc1)
	goRoute.Waypoints.Add(goLoc2)
	goRoute.Calculate()	&& Compute the route and get directions.
	myDistance2 = goRoute.Distance
ELSE
	myDistance2=0
ENDIF 
NOTE ? zipcode1+"  "+zipcode2+"  Distance1"+STR(myDistance1)+"  Distance2"+STR(myDistance2)+STR(RECNO())+"  of  "+STR(RECCOUNT())
goMap.ActiveRoute.Clear()

goRoute=null
gomap.Application.ActiveMap.Saved=.t.
gomap.Application.Quit()
goMap=null

IF nAirORDrive=0
	RETURN (myDistance1)
ELSE
	RETURN (myDistance2)
ENDIF

Steve Bowman
Independent Technology, Inc.
CA, USA
 
duh... forgot to add
the above post requires MapPoint

Steve Bowman
Independent Technology, Inc.
CA, USA
 
Steve,
When I run this code passing in proper parameters I get an error on the line Ole Error code Type mismatch:

goLoc1 = goMap.Find(m.cLocation1)

I am using Mappoint 2004 (11.00.18.1900)
 
what is the value of m.cLocation1 ?


Steve Bowman
Independent Technology, Inc.
CA, USA
 
you need commas between the street,city,state,zip
? getMiles("123 street,anycity,anystate,anyzip",("456 street,anycity,anystate,anyzip",0)


Steve Bowman
Independent Technology, Inc.
CA, USA
 
sTEVE,

Works like a charm with the commas. Thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top