Google GeoCoding

u137a

Member
Licensed User
Longtime User
Hi all.
I am just writing a wee app. to get me started on B4A.
I have the lat and long values from the GPS but what I want to do is have my wee app. send a request to the google geocoder and get back the address on request.
I found this URL Using Google Maps in Android | mobiForge but of course its java code.
I know you can use the Google API which I have used on a webpage a few times, but that requires you pass in a key which is registered to the domain the request is coming from which is not ideal in my situation

So just wondered if anyone else has some code for this or if B4A has a library for this already that I have missed.

Thanks, Jeremy
 

alfcen

Well-Known Member
Licensed User
Longtime User
Hi,
Here is a code snippet which acquires an address and weather info (provided the service servers are not busy). For this to work you need to add the HTTP and XmlSax library. Just replace the coordinates in the http query with yours.

B4X:
'Activity module
Sub Process_Globals
   Dim HttpClient1 As HttpClient
   Dim Parser As SaxParser
End Sub

Sub Globals
   Dim Label1 As Label
   Dim wm As String
End Sub

Sub Activity_Create(FirstTime As Boolean)
    If FirstTime Then
      HttpClient1.Initialize("HttpClient1")
      Parser.Initialize
   End If
   Activity.LoadLayout("layout1")
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Label1_Click
   wm = ""
   Dim request As HttpRequest
   request.InitializeGet("http://maps.google.com/maps/geo?output=xml&oe=utf-8&ll=-26,132")
   request.Timeout = 10000 'set timeout to 10 seconds
   If HttpClient1.Execute(request, 1) = False Then Return 'Will be false if there is already a running task (with the same id).
   ProgressDialogShow("Calling address server...")   
   request.InitializeGet("http://ws.geonames.org/findNearByWeatherXML?lat=-26&lng=132")
   request.Timeout = 10000 'set timeout to 10 seconds
   If HttpClient1.Execute(request, 2) = False Then Return 'Will be false if there is already a running task (with the same id).
   ProgressDialogShow("Calling weather server...")
End Sub

Sub HttpClient1_ResponseSuccess (Response As HttpResponse, TaskId As Int)
   ProgressDialogHide
   Dim result As InputStream
   result = Response.GetInputStream 'GetString("UTF8") 'Convert the response to a string
   Parser.Parse(result, "Parser")
   result.Close
   Label1.Text = wm   
End Sub

Sub HttpClient1_ResponseError (Reason As String, StatusCode As Int, TaskId As Int)
   ProgressDialogHide
   If StatusCode = -1 Then msg = "The phone is offline"
   'If reason <> Null Then msg = msg & CRLF & Reason
   ToastMessageShow (Reason, True)
End Sub

Sub Parser_StartElement (Uri As String, Name As String, Attributes As Attributes)

End Sub

Sub Parser_EndElement (Uri As String, Name As String, Text As StringBuilder)
   If Parser.Parents.IndexOf("Placemark") > -1 Then
      If Name = "address" Then
         If wm.Length > 0 Then Return
          wm = "Address: " & Text.ToString & CRLF
      End If
   End If   
   If Parser.Parents.IndexOf("geonames") > -1 Then
      If Name = "temperature" Then
          wm = wm & "Temperature: " & Text.ToString & Chr(176) & "C" & CRLF
         temp = Text.ToString & Chr(176) & "C" & CRLF
      Else If Name = "humidity" Then
         wm = wm & "Humidity: " & Text.ToString & "%RH" & CRLF
         temp = temp & Text.ToString & "%RH"
      Else If Name = "seaLevelPressure" Then
         wm = wm & "Pressure: " & Text.ToString & "mb" & CRLF         
      Else If Name = "windDirection" Then
         wm = wm & "Wind direction: " & Text.ToString & Chr(176) & CRLF
      Else If Name = "windSpeed" Then
         wm = wm & "Wind speed: " & Text.ToString & "km/h" & CRLF
      Else If Name = "clouds" Then
         a = Text.ToString
         a = a.ToUpperCase.CharAt(0) &  a.SubString(1)
         wm = wm & a & CRLF   
      End If
   End If
End Sub
 
Upvote 0

u137a

Member
Licensed User
Longtime User
Hi all.
I am just writing a wee app. to get me started on B4A.
I have the lat and long values from the GPS but what I want to do is have my wee app. send a request to the google geocoder and get back the address on request.
I found this URL Using Google Maps in Android | mobiForge but of course its java code.
I know you can use the Google API which I have used on a webpage a few times, but that requires you pass in a key which is registered to the domain the request is coming from which is not ideal in my situation

So just wondered if anyone else has some code for this or if B4A has a library for this already that I have missed.

Thanks, Jeremy
?? - THIS IS NOT MY POST!

Admin - could you pls check if there are duplicate usernames or database corruption.

Thanks.
 
Upvote 0

mistermentality

Active Member
Licensed User
Longtime User
Upvote 0
Top