B4J Question Suggest, Auto-complete textfield with Addresses/Locations (Map, Google)

Magma

Expert
Licensed User
Longtime User
Hi there,

If anyone goes to Google Maps and start typing will see suggested address (with no of street too).. Is there anything that i can to have it at my textfields/combobox too... ??

And the best part when doing from Google is when you zoomed at Town, city, village / search is much better...

Using a service like Mapnik (that is free or Google with a key if needed)...

For b4a - i ve found a solution:

For b4j - found too with a little fixes will be ok:
But how on this can put limits (lower lat-lon, higher lat-lon) ?
 
Last edited:

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

Magma

Expert
Licensed User
Longtime User
Hey i ve found your example for b4a... and just change it a little...

but where i can put limits and how i will change the language of result ? (returns the address in english - i want to return in greek)...

B4X:
Sub search4locations(addr As String)
    Dim Key As String = "MYKEY"
    Dim postString As String
    Dim su As StringUtils
    Dim dir As HttpJob
    dir.Initialize("",Me)
      
    postString="address=" & su.EncodeUrl(addr,"UTF8") & "&Key=" & Key
  

    Log("https://maps.googleapis.com/maps/api/geocode/json?"&postString)
  

    dir.download("https://maps.googleapis.com/maps/api/geocode/json?" & postString)
    Wait For (dir) JobDone(dir As HttpJob)
    If dir.Success Then
   
        Dim jp As JSONParser
        jp.Initialize(dir.GetString)
        Dim m As Map = jp.NextObject
        Dim results As List = m.Get("results")
        For Each colresults As Map In results
        Dim formatted_address As String = colresults.Get("formatted_address")
        Dim types As List = colresults.Get("types")
        For Each coltypes As String In types
        Next
        Dim geometry As Map = colresults.Get("geometry")
        Dim viewport As Map = geometry.Get("viewport")
        Dim southwest As Map = viewport.Get("southwest")
        Dim lng As Double = southwest.Get("lng")
        Dim lat As Double = southwest.Get("lat")
        Dim northeast As Map = viewport.Get("northeast")
        Dim lng As Double = northeast.Get("lng")
        Dim lat As Double = northeast.Get("lat")
        Dim location As Map = geometry.Get("location")
        Dim lng As Double = location.Get("lng")
        Dim lat As Double = location.Get("lat")
        Dim location_type As String = geometry.Get("location_type")
        Dim address_components As List = colresults.Get("address_components")
        For Each coladdress_components As Map In address_components
            Dim types As List = coladdress_components.Get("types")
            For Each coltypes As String In types
            Next
            Dim short_name As String = coladdress_components.Get("short_name")
            Dim long_name As String = coladdress_components.Get("long_name")
        Next
        Dim place_id As String = colresults.Get("place_id")
    Next
    Dim status As String = m.Get("status")
    End If
    dir.Release

        Log(m)


End Sub
 
Upvote 0

Magma

Expert
Licensed User
Longtime User
B4X:
Sub search4locations2(addr As String)
    Dim Key As String = "mykey"
    Dim bounds As String ="40.868825,23.250299|41.438562,24.033075" 'limits
    Dim postString As String
    Dim su As StringUtils
    Dim dir As HttpJob
    dir.Initialize("",Me)

      
    'postString="address=" & addr.Replace(" ","+").trim &"&bounds=" & bounds & "&key=" & Key
  

    'Log("https://maps.googleapis.com/maps/api/geocode/json?"&postString)
  

    dir.download2("https://maps.googleapis.com/maps/api/geocode/json",Array As String("address",addr,"bounds",bounds,"language","el","country","gr","key",Key))
    Wait For (dir) JobDone(dir As HttpJob)
    If dir.Success Then
   
        Dim jp As JSONParser
        jp.Initialize(dir.GetString)
        Dim m As Map = jp.NextObject
        Dim results As List = m.Get("results")
        For Each colresults As Map In results
        Dim formatted_address As String = colresults.Get("formatted_address")
        Dim types As List = colresults.Get("types")
        For Each coltypes As String In types
        Next
        Dim geometry As Map = colresults.Get("geometry")
        Dim viewport As Map = geometry.Get("viewport")
        Dim southwest As Map = viewport.Get("southwest")
        Dim lng As Double = southwest.Get("lng")
        Dim lat As Double = southwest.Get("lat")
        Dim northeast As Map = viewport.Get("northeast")
        Dim lng As Double = northeast.Get("lng")
        Dim lat As Double = northeast.Get("lat")
        Dim location As Map = geometry.Get("location")
        Dim lng As Double = location.Get("lng")
        Dim lat As Double = location.Get("lat")
        Dim location_type As String = geometry.Get("location_type")
        Dim address_components As List = colresults.Get("address_components")
        For Each coladdress_components As Map In address_components
            Dim types As List = coladdress_components.Get("types")
            For Each coltypes As String In types
            Next
            Dim short_name As String = coladdress_components.Get("short_name")
            Dim long_name As String = coladdress_components.Get("long_name")
        Next
        Dim place_id As String = colresults.Get("place_id")
    Next
    Dim status As String = m.Get("status")
    End If
    dir.Release

  log(formatted_address)
   

End Sub
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
its and how i will change the language of result ?
see documentation. There is a parameter for the language

Optional parameters in a geocoding request:

  • bounds — The bounding box of the viewport within which to bias geocode results more prominently. This parameter will only influence, not fully restrict, results from the geocoder. (For more information see Viewport Biasing below.)
  • language — The language in which to return results.
    • See the list of supported languages. Google often updates the supported languages, so this list may not be exhaustive.
    • If language is not supplied, the geocoder attempts to use the preferred language as specified in the Accept-Language header, or the native language of the domain from which the request is sent.
    • The geocoder does its best to provide a street address that is readable for both the user and locals. To achieve that goal, it returns street addresses in the local language, transliterated to a script readable by the user if necessary, observing the preferred language. All other addresses are returned in the preferred language. Address components are all returned in the same language, which is chosen from the first component.
    • If a name is not available in the preferred language, the geocoder uses the closest match.
    • The preferred language has a small influence on the set of results that the API chooses to return, and the order in which they are returned. The geocoder interprets abbreviations differently depending on language, such as the abbreviations for street types, or synonyms that may be valid in one language but not in another. For example, utca and tér are synonyms for street in Hungarian.
  • region — The region code, specified as a ccTLD ("top-level domain") two-character value. This parameter will only influence, not fully restrict, results from the geocoder. (For more information see Region Biasing below.)
  • components — The component filters, separated by a pipe (|). Each component filter consists of a component:value pair and will fully restrict the results from the geocoder. For more information see Component Filtering, below.
 
Upvote 0
Top