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

Programmatically Find Latitude and Longitude

Internet

Programmatically Find Latitude and Longitude

by  wgcs  Posted    (Edited  )
Do you want to find the Latitude & Longitude for a whole bunch of addresses?
Microsoft's TerraServer provides this information as a demonstration of SqlServer and with a little VFP programming, you can make use of it from within a program.

CDyne also provides this service, though for a fee. It seems they allow you to try their webservice for free for about 5 minutes before locking you out.

You can use your own method of retrieving the web page, or see faq184-3019 for the ReadUrl function.

Code:
LOCAL lcSt, lcMethod, lcLong, lcLat, lcLong1, lcLat1
ON KEY LABEL ESC SUSPEND
SET DECIMALS TO 8
USE Addresses
lcMethod  = 'TERRASERVER'
*lcMethod  = 'CDYNE'

SCAN
  lcSt = UPPER( ALLTRIM(Address.hse_num)+'+'+STRTRAN(ALLTRIM(Address.Street),' ','+') )
  
  ? "Processing Record ",RECNO(),"of",RECCOUNT(), lcSt, Address.zipcode, address.State
  
  DO CASE
    CASE lcMethod='TERRASERVER'
      * You may have to log in to terraserver.microsoft.com and get a VIEWSTATE value for yourself:
      lcGetUrl = 'http://www.terraserver.microsoft.com/address.aspx?__VIEWSTATE=dDwxNzM4NTI3NTc7O2w8U2VhcmNoOz4%2B';
                 +'&Street='+lcSt;
                 +'&City=';
                 +'&State='+ALLTRIM(address.State);
                 +'&ZipCode='+ALLTRIM(address.zipcode);
                 +'&Search.x=0&Search.y=0'
                 
      ReadUrl( lcGETURL )
  
      lcLong   = SUBSTR(pineview.Resp, ATC('&lon=',pineview.Resp)+5)
      lcLong1  = LEFT( lcLong,        ATC('&',lcLong)-1 )
      
      lcLat    = SUBSTR(pineview.Resp, ATC('&lat=',pineview.Resp)+5)
      lcLat1   = LEFT( lcLat,         ATC('&',lcLat)-1 )
      REPLACE address.Longitude WITH VAL(lcLong1), ;
              address.Latitude  WITH VAL(lcLat1)
    
  CASE lcMethod='CDYNE'
    * This is a webservice that can do the same thing, only with much simpler code,
    *   and it offers about 5 minutes of free trial on license key "0" before locking you out:
    * See http://ws.cdyne.com

    lcGetUrl = "http://ws.cdyne.com/psaddress/addresslookup.asmx/AdvancedCheckAddress" ;
      +'&AddressLine='+lcSt;
      +'&AddressLine2=';
      +'&ZipCode='+ALLTRIM(address.zipcode);
      +'&City=';
      +'&StateAbbrev='+ALLTRIM(address.state);
      +'&LicenseKey=0';
      +'&Invoke=submit'
    
    ReadUrl( lcGETURL )
    lcXML = SUBSTR( bayview.resp, AT(CHR(13)+CHR(10)+CHR(13),bayview.resp)+4 )
    XMLTOCURSOR(lcXML,'Response')
    REPLACE Addresses.Longitude WITH response.AvgLongitude, ;
            Addresses.Latitude  WITH response.AvgLatitude
    lcLong1 = response.AvgLongitude
    lcLat1  = response.AvgLatitude
  ENDCASE
  
  ??" Long=",lcLong1," Lat=",lcLat1
  FLUSH
ENDSCAN
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top