Android Question Reverse geocode a longitude and latitude

Discussion in 'Android Questions' started by trueboss323, Mar 17, 2015.

  1. trueboss323

    trueboss323 Active Member Licensed User

    Hello,

    I would like to reverse geocode a given longitute and laditute and return just the country name. I was unable to find a simple solution to this in the forums, so hopefully someone can be of help here.

    I am using this Geocode library
    http://www.b4x.com/android/forum/threads/geocoder-library.17115/

    Can anyone give a coding example as to how I can do that?
     
    Last edited: Mar 17, 2015
  2. mark35at

    mark35at Well-Known Member Licensed User

  3. trueboss323

    trueboss323 Active Member Licensed User

    I apologize for that. It is the coding that I need help with

    Code:
    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
     
  4. peacemaker

    peacemaker Well-Known Member Licensed User

    Recently, the online service (of this lib) does not work well, as i remember.
     
  5. mark35at

    mark35at Well-Known Member Licensed User

    Without trying any code, it looks like you are missing a sub. Looking at the example from Warwound:

    Code:
    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
     
  6. trueboss323

    trueboss323 Active Member Licensed User

    Thank you, I made a few changes to your code and it works great now!

    Code:
    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: Mar 18, 2015
  7. warwound

    warwound Expert Licensed User

  8. peacemaker

    peacemaker Well-Known Member Licensed User

    Yes, for ex.:

    Code:
    Dim loc As Locale
    Geocoder1.Initialize3(
    "Geocoder1", loc.GetDefault.GetLanguage, loc.GetDefault.GetCountry)
    Resources lib is required.
    But recently service returned empty :-(
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice