Improve company productivity with a Business Account.Sign Up

x
?
Solved

VB6 - Query latitude and longitude with Google map issue

Posted on 2013-06-25
6
Medium Priority
?
1,794 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 200 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
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
LVL 15

Accepted Solution

by:
eemit earned 1800 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

588 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