Android Question Reverse geocode a longitude and latitude

Mark Read

Well-Known Member
Licensed User
Longtime User
Upvote 0

trueboss323

Active Member
Licensed User
Longtime User
I apologize for that. It is the coding that I need help with

B4X:
Sub Process_Globals
    Private FusedLocationProvider1 As FusedLocationProvider
    Private LastLocation As Location
    Dim geoaddress As Address
End Sub

Sub Globals

End Sub

Sub Activity_Create(FirstTime As Boolean)
    '    **    IMPORTANT see manifest for required entries    **
    If FirstTime Then
        FusedLocationProvider1.Initialize("FusedLocationProvider1")
    End If
   
End Sub

Sub Activity_Resume
    '    attempt to connect to the location services
    '    after calling Connect we are waiting for either ConnectionFailed or ConnectionSuccess events
    FusedLocationProvider1.Connect
End Sub

Sub Activity_Pause (UserClosed As Boolean)
    FusedLocationProvider1.Disconnect
End Sub

Sub FusedLocationProvider1_ConnectionFailed(ConnectionResult1 As Int)
    Log("FusedLocationProvider1_ConnectionFailed")
   
    '    the FusedLocationProvider ConnectionResult object contains the various CoonectionResult constants
   
    Select ConnectionResult1
        Case FusedLocationProvider1.ConnectionResult.NETWORK_ERROR
            '    a network error has occurred, this is likely to be a recoverable error
            '    so try to connect again
            FusedLocationProvider1.Connect
        Case Else
            '    TODO handle other errors
    End Select
End Sub

Sub FusedLocationProvider1_ConnectionSuccess
    Log("FusedLocationProvider1_ConnectionSuccess")

    Dim LastKnownLocation As Location
    LastKnownLocation = FusedLocationProvider1.GetLastKnownLocation

    If LastKnownLocation.IsInitialized Then
        GeocodeLocation(LastKnownLocation)
    Else
        Dim LocationRequest1 As LocationRequest
        LocationRequest1.Initialize
        LocationRequest1.SetInterval(1000)    '    1000 milliseconds
        LocationRequest1.SetPriority(LocationRequest1.Priority.PRIORITY_NO_POWER)
        LocationRequest1.SetSmallestDisplacement(1)    '    1 meter
        FusedLocationProvider1.RequestLocationUpdates(LocationRequest1)
    End If
End Sub

Sub FusedLocationProvider1_ConnectionSuspended(SuspendedCause1 As Int)
    Log("FusedLocationProvider1_ConnectionSuspended")
   
    '    the FusedLocationProvider SuspendedCause object contains the various SuspendedCause constants
   
    Select SuspendedCause1
        Case FusedLocationProvider1.SuspendedCause.CAUSE_NETWORK_LOST
            '    TODO take action
        Case FusedLocationProvider1.SuspendedCause.CAUSE_SERVICE_DISCONNECTED
            '    TODO take action
    End Select
End Sub

Sub FusedLocationProvider1_LocationChanged(Location1 As Location)
    Log("FusedLocationProvider1_LocationChanged")

    FusedLocationProvider1.RemoveLocationUpdates
    GeocodeLocation(Location1)
End Sub

Sub GeocodeLocation(Location1 As Location)
' This is the part I need help with

    Log("GeocodeLocation: "&Location1.Latitude&", "&Location1.Longitude)
    '    here you can get the Location Latitude and Longitude properties and use the Geocoder library to perform a reverse geocode
    '    if the reverse geocode is successful you will have the country that the device is located in


Dim lc As Geocoder
lc.GetFromLocation(Location1.Latitude, Location1.Longitude,1,Null)

lc.Initialize("lc")
Log("Country is " & lc)

End Sub
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
Recently, the online service (of this lib) does not work well, as i remember.
 
Upvote 0

Mark Read

Well-Known Member
Licensed User
Longtime User
Without trying any code, it looks like you are missing a sub. Looking at the example from Warwound:

B4X:
Sub Button1_Click
    ResultsList.Clear
   
    Dim Latitude, Longitude As Double
    Latitude=LatitudeEdit.Text
    Longitude=LongitudeEdit.Text
   
    '    MaxResults parameter hardcoded to 5 for the demo
    Dim MaxResults As Int
    MaxResults=5
   
    Geocoder1.GetFromLocation(Latitude, Longitude, 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)
            Log("Address1.AddressLines.Size="&Address1.AddressLines.Size)
            If Address1.AddressLines.Size>0 Then
                ResultsList.AddTwoLines(Address1.AddressLines.Get(0), Address1.AddressLines.Get(0))
            End If
        Next
    Else
        Msgbox("GetFromLocation", "No Address matched the Latitude and Longitude")
    End If
End Sub
 
Upvote 0

trueboss323

Active Member
Licensed User
Longtime User
Thank you, I made a few changes to your code and it works great now!

B4X:
Sub Process_Globals
    Private FusedLocationProvider1 As FusedLocationProvider
    Private LastLocation As Location

    Dim geoaddress As Address
End Sub

Sub Globals

    Dim Latitude, Longitude As Double
    Dim lc As Geocoder
        Dim ResultsList As ListView
End Sub

Sub Activity_Create(FirstTime As Boolean)
    '    **    IMPORTANT see manifest for required entries    **
    Activity.LoadLayout("Main")
    ResultsList.Initialize("ResultsList")
    If FirstTime Then
        FusedLocationProvider1.Initialize("FusedLocationProvider1")
    End If
  
End Sub

Sub Activity_Resume
    '    attempt to connect to the location services
    '    after calling Connect we are waiting for either ConnectionFailed or ConnectionSuccess events
    FusedLocationProvider1.Connect
End Sub

Sub Activity_Pause (UserClosed As Boolean)
    FusedLocationProvider1.Disconnect
End Sub

Sub FusedLocationProvider1_ConnectionFailed(ConnectionResult1 As Int)
    Log("FusedLocationProvider1_ConnectionFailed")
   
    '    the FusedLocationProvider ConnectionResult object contains the various CoonectionResult constants
   
    Select ConnectionResult1
        Case FusedLocationProvider1.ConnectionResult.NETWORK_ERROR
            '    a network error has occurred, this is likely to be a recoverable error
            '    so try to connect again
            FusedLocationProvider1.Connect
        Case Else
            '    TODO handle other errors
    End Select
End Sub

Sub FusedLocationProvider1_ConnectionSuccess
    Log("FusedLocationProvider1_ConnectionSuccess")

    Dim LastKnownLocation As Location
    LastKnownLocation = FusedLocationProvider1.GetLastKnownLocation

    If LastKnownLocation.IsInitialized Then
        GeocodeLocation(LastKnownLocation)
    Else
        Dim LocationRequest1 As LocationRequest
        LocationRequest1.Initialize
        LocationRequest1.SetInterval(1000)    '    1000 milliseconds
        LocationRequest1.SetPriority(LocationRequest1.Priority.PRIORITY_NO_POWER)
        LocationRequest1.SetSmallestDisplacement(1)    '    1 meter
        FusedLocationProvider1.RequestLocationUpdates(LocationRequest1)
    End If
End Sub

Sub FusedLocationProvider1_ConnectionSuspended(SuspendedCause1 As Int)
    Log("FusedLocationProvider1_ConnectionSuspended")
   
    '    the FusedLocationProvider SuspendedCause object contains the various SuspendedCause constants
   
    Select SuspendedCause1
        Case FusedLocationProvider1.SuspendedCause.CAUSE_NETWORK_LOST
            '    TODO take action
        Case FusedLocationProvider1.SuspendedCause.CAUSE_SERVICE_DISCONNECTED
            '    TODO take action
    End Select
End Sub

Sub FusedLocationProvider1_LocationChanged(Location1 As Location)
    Log("FusedLocationProvider1_LocationChanged")

    FusedLocationProvider1.RemoveLocationUpdates
    GeocodeLocation(Location1)
End Sub

Sub GeocodeLocation(Location1 As Location)
    Log("GeocodeLocation: "&Location1.Latitude&", "&Location1.Longitude)
    '    here you can get the Location Latitude and Longitude properties and use the Geocoder library to perform a reverse geocode
    '    if the reverse geocode is successful you will have the country that the device is located in

lc.GetFromLocation(Location1.Latitude, Location1.Longitude,1,Null)
Longitude = Location1.Longitude
Latitude = Location1.Latitude

lc.Initialize("lc")

Log(lc)
End Sub



Sub Button1_Click

    ResultsList.Clear
  

  
    '    MaxResults parameter hardcoded to 5 for the demo
    Dim MaxResults As Int
    MaxResults=5
 
   lc.GetFromLocation(Latitude, Longitude, MaxResults, Null)
   If lc.IsInitialized = False Then
   Msgbox("Location not found, please check that your Location Services and Data connection are working","")
   End If
End Sub
Sub lc_GeocodeDone(Results() As Address, Tag As Object)
   Try
   ' If Results.Length>0 Then
        
        Dim Address1 As Address
        Dim i As Int

            Address1=Results(i)
    
         If Address1.AddressLines.Size>0 Then
                Log("Country: "&Address1.CountryName)
       
            End If
  
  '  Else
    '    Msgbox("GetFromLocation", "No Address matched the Latitude and Longitude")
  '  End If
  Catch
  End Try
End Sub

I also stumbled into a problem. I noticed that when the language in device settings is changed, the country name gets localized. Is there anyway I can always display the country names in English, regardless of what language settings are chose?
 
Last edited:
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
Look at the Geocoder object's various Initialize methods

Yes, for ex.:

B4X:
Dim loc As Locale
Geocoder1.Initialize3("Geocoder1", loc.GetDefault.GetLanguage, loc.GetDefault.GetCountry)

Resources lib is required.
But recently service returned empty :-(
 
Upvote 0
Top