Android Question Getting Lat/Lon by Geocoder

clooney48

Active Member
Licensed User
Longtime User
I try to get Lat/Lon from an address using only the relevant code of the Geocoder example. The problem is that the procedure starts with the sub Geo_Start but doesn´t continue with the sub Geocoder1_GeocodeDone. In the original programm it works this way.

B4X:
Sub Geo_Start
   Dim LocationName As String
   LocationName = Addr2
   Geocoder1.GetFromLocationName(LocationName, MaxResults, Null)
End Sub

Sub Geocoder1_GeocodeDone(Results() As Address, Tag As Object)
   If Results.Length>0 Then
     Dim Address1 As Address
     Dim i As Int
     For i=0 To Results.Length-1
       Address1=Results(i)
       'ResultsList.AddSingleLine("("&Address1.Latitude&", "&Address1.Longitude&")")
       Lat1 = Address1.Latitude
       Lon1 = Address1.Longitude
     Next
   Else
     Msgbox("GetFromLocation", "No Latitude and Longitude found for the LocationName")
   End If
End Sub

In the original sample the line in sub Geo_Start contains "Main." I don´t know why this reference to the main page is necessary:

B4X:
Main.Geocoder1.GetFromLocationName(LocationName, MaxResults, Null)
 

clooney48

Active Member
Licensed User
Longtime User
How did you initialize Geocoder1?
Hi Erel, I initialized it in Activity_Create. Please see the complete code:
B4X:
Sub Process_Globals
   Dim cursor1 As Cursor
   Dim SQL1 As SQL
   Dim Addr2, Lat1, Lat2, Lon1, Lon2 As String
End Sub

Sub Globals
   Dim LVDb As ListView
   Dim Dist 
   Dim Geocoder1 As Geocoder
End Sub

Sub Activity_Create(FirstTime As Boolean)
     Geocoder1.Initialize("Geocoder1")
     Activity.LoadLayout("DBListView")
     If SQL1.IsInitialized = False Then
          SQL1.Initialize(File.DirInternal, "mydb_db.sql", False)
     End If

   LVDb.Clear
   LVDb.SingleLineLayout.ItemHeight = 120
   LVDb.SingleLineLayout.Label.TextSize = 20
   LVDb.SingleLineLayout.Label.TextColor = Colors.White
   LVDb.SingleLineLayout.Label.Color = Colors.Black
   cursor1 = SQL1.ExecQuery("SELECT * FROM tblUsers")
   For i = 0 To cursor1.RowCount - 1
     cursor1.Position = i
     Addr2 = cursor1.GetString("Addr")
     Geo_Start
     LVDb.AddSingleLine(cursor1.GetString("ID")& ") " & cursor1.GetString("Name") & "  " & Lat1 & "  " & Lon1)
   Next
   
End Sub
Sub Geo_Start
  Dim LocationName As String
  LocationName = Addr2
  Geocoder1.GetFromLocationName(LocationName, MaxResults, Null)
End Sub

Sub Geocoder1_GeocodeDone(Results() As Address, Tag As Object)
  If Results.Length>0 Then
    Dim Address1 As Address
    Dim i As Int
    For i=0 To Results.Length-1
      Address1=Results(i)
      Lat1 = Address1.Latitude
      Lon1 = Address1.Longitude
      Next
  Else
    Msgbox("GetFromLocation", "No Latitude and Longitude found for the LocationName")
  End If
End Sub
 
Upvote 0

clooney48

Active Member
Licensed User
Longtime User
Hi Erel!
Now I found out that this code works when I start Geo_Start with a button (Geo_Start_Click). Then Geo_Start continues with Geocoder1_GeocodeDone. But this doesn´t help me. I have to start it automatically in Activity_Create(FirstTime As Boolean).
Is there a way to do it?

Best regards
 
Last edited:
Upvote 0
Top