Solved

VB6 - Query latitude and longitude with Google map issue

Posted on 2013-06-25
6
1,657 Views
Last Modified: 2013-06-26
Hello all,

I have a function inside my VB6 app that will look at the address / city / state and postal code and then, use Google map to get the latitude and longitude.

When i'm home, it work great but when i"m log on a server, it does not work at all.

Would you know what it it?

How can i make it work at home and on the server also?

Thanks again for your help.

Module:
Public Declare Function InternetOpen Lib "wininet.dll" Alias "InternetOpenA" (ByVal sAgent As String, ByVal lAccessType As Long, ByVal sProxyName As String, ByVal sProxyBypass As String, ByVal lFlags As Long) As Long
Public Declare Function InternetOpenUrl Lib "wininet.dll" Alias "InternetOpenUrlA" (ByVal hInternetSession As Long, ByVal sURL As String, ByVal sHeaders As String, ByVal lHeadersLength As Long, ByVal lFlags As Long, ByVal lContext As Long) As Long
Public Declare Function InternetReadFile Lib "wininet.dll" (ByVal hFile As Long, ByVal sBuffer As String, ByVal lNumBytesToRead As Long, lNumberOfBytesRead As Long) As Integer
Public Declare Function InternetCloseHandle Lib "wininet.dll" (ByVal hInet As Long) As Integer
Public Const IF_NO_CACHE_WRITE = &H4000000
Private Const BUFFER_LEN = 8192

Public Function GetUrlSource(sURL As String) As String
 Dim sBuffer As String * BUFFER_LEN, iResult As Integer, sData As String
 Dim hInternet As Long, hSession As Long, lReturn As Long
'=================================================
'get the handle of the current internet connection
'=================================================
 hSession = InternetOpen("vb wininet", 1, vbNullString, vbNullString, 0)
'=========================
'get the handle of the url
'=========================
 If hSession Then
  hInternet = InternetOpenUrl(hSession, sURL, vbNullString, 0, IF_NO_CACHE_WRITE, 0)
 End If
'=================
'READ WEB RESPONSE
'=================
 If hInternet Then
 '=====================================
 'GET FIRST CHUNK OF DATA AND BUFFER IT
 '=====================================
  iResult = InternetReadFile(hInternet, sBuffer, BUFFER_LEN, lReturn)
  sData = sBuffer
 '==============================================
 'LOOP THROUGH THE BALANCE AND ADD TO THE BUFFER
 '==============================================
  Do While lReturn <> 0
   iResult = InternetReadFile(hInternet, sBuffer, BUFFER_LEN, lReturn)
   sData = sData + Mid(sBuffer, 1, lReturn)
  Loop
 End If
'==========================================
'CLOSE THE CONNECTION AND RETURN THE RESULT
'==========================================
 iResult = InternetCloseHandle(hInternet)
 GetUrlSource = sData
End Function

Open in new window



AskGoogleButton:
 Latitude(1) = "": Latitude(2) = ""
 Longitude(1) = "": Longitude(2) = ""
 DoEvents
'==========================================
'CHECK IF USER FILLED IN THE ADDRESS FIELDS
'==========================================
 If MissingData Then
  MsgBox "Please fill in the form correctly.", vbApplicationModal + vbExclamation, "Whoops!"
  Exit Sub
 End If
 AskGoogleButton.Enabled = False
 Screen.MousePointer = 11
 Dim NotFound As Boolean
 For i = 1 To 2
  NotFound = True
  WebURL$ = "http://maps.google.com/maps?q="
  WebURL$ = WebURL$ & Trim$(Street(i)) & ",+"
  WebURL$ = WebURL$ & Trim$(City(i)) & ",+"
  WebURL$ = WebURL$ & Trim$(State(i)) & ",+"
  WebURL$ = WebURL$ & Trim$(Zip(i))
  WebURL$ = Replace$(WebURL$, " ", "+")
  GoogleWebPage$ = GetUrlSource(WebURL$)
  A$ = LCase$(GoogleWebPage$)
 '=======================================================
 'EXTRACT THE LATITUDE AND LONGITUDE FROM GOOGLE WEB PAGE
 '=======================================================
  GC = InStr(A$, ":{lat:") + 2
  If GC > 2 Then
   GC2 = InStr(GC, A$, "},")
   LC = GC2 - GC
   If LC > 20 And LC < 200 Then
    A$ = Mid$(A$, GC + 4, LC - 6)
    Comma = InStr(A$, ",")
    If Comma Then
     Lat$ = Left$(A$, Comma - 1) & String$(12, 48)
     Lat$ = Left$(Lat$, 12)
     Lon$ = Right$(A$, Len(A$) - (Comma + 4)) & String$(12, 48)
     Lon$ = Left$(Lon$, 12)
     If Abs(Val(Lat$)) > 0 And Abs(Val(Lon$)) > 0 Then
      Latitude(i) = Lat$
      Longitude(i) = Lon$
      NotFound = False
     End If
    End If
   End If
  End If
  DoEvents
 Next i
ReturnResult:
 Screen.MousePointer = Default
 If NotFound Then
  MsgBox "NO MAP COORDINATES HAVE BEEN FOUND!", vbApplicationModal + vbExclamation, "ADDRESS???"
  Beep
 Else ' Watch for my next upgrade - ok? (Dinner's waiting)
 '============================================================================
 'Why not put some MORE FUN into this and calculate the distance between them?
 '============================================================================
 End If
 AskGoogleButton.Enabled = True
 Exit Sub
BolloxedData:
 NotFound = True
 Resume ReturnResult:

Open in new window

0
Comment
Question by:Wilder1626
  • 4
6 Comments
 
LVL 7

Assisted Solution

by:dec0mpile
dec0mpile earned 50 total points
ID: 39274588
What is the error you are getting?

Many servers have security that prevents you from browsing the internet, so your script may be getting blocked by the server security from accessing the google maps site. Investigate the server firewall and internet access.

Which version of Windows server are you running this script on?

In later versions of Windows Sever the User Account Control prevents VBS scripts from performing many functions. Try to right click on the script and select "Run As Administrator" or disable UAC to rule out this possibility.
0
 
LVL 11

Author Comment

by:Wilder1626
ID: 39274660
If i go manually on the web, i'm able to go on Google map.

It is only when i use the VB6 app.

what i have as a result is NO MAP COORDINATES HAVE BEEN FOUND! but this is because i don't link to the server i guess.
0
 
LVL 11

Author Comment

by:Wilder1626
ID: 39276498
do you know another way that it would work?
Since i can go manually in Google map, i should be able to do the same thing from my app. Right?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 15

Accepted Solution

by:
eemit earned 450 total points
ID: 39277310
Try this:

' Use registry access settings.
Private Const INTERNET_OPEN_TYPE_PRECONFIG = 0  'Retrieves the proxy or direct configuration from the registry.

Open in new window


Change (in Function GetUrlSource):
hSession = InternetOpen("vb wininet", 1, vbNullString, vbNullString, 0)

Open in new window

To:
hSession = InternetOpen("vb wininet", INTERNET_OPEN_TYPE_PRECONFIG, vbNullString, vbNullString, 0)

Open in new window

0
 
LVL 11

Author Comment

by:Wilder1626
ID: 39277994
Oh, i think it works now.

Let me do some validation and i will let you know.

Thanks.
0
 
LVL 11

Author Closing Comment

by:Wilder1626
ID: 39279894
Hi again

It is now working great

Thanks again for your help
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question