B4A Class GooglePlaceSearchBar

This class is based on this custom view: https://www.b4x.com/android/forum/t...google-place-autocomplete-view.81976/#content
Feel free to edit and improve it as you want. This class is a beta version, i will share an improved version when i fix new bugs

As explained from Erel these 3 step are required:
- In order to use Google Places API you need to create a project in Google developer console: https://console.developers.google.com/
- Enable Google Places API Web Service api.
- Create an API key and set it using
B4X:
GoogleSearchBar1.ApiKey = Yourkey

In order to get coordinate from Place Found you need also to enable Google Maps Geocoding API

Code to get place is simple
B4X:
Sub GoogleSearchBar1_PlaceFound (PlaceID As String)
    Dim j As HttpJob
    j.Initialize("", Me)
    j.Download2("https://maps.googleapis.com/maps/api/geocode/json",Array As String("place_id", PlaceID, "key", key))
    Wait For (j) JobDone (j As HttpJob)
    If j.Success Then
        Dim parser As JSONParser
        parser.Initialize(j.GetString)
        Dim root As Map = parser.NextObject
        Dim results As List = root.Get("results")
        Dim colres As Map = results.Get(0)
        Dim geometry As Map = colres.Get("geometry")
        Dim location As Map = geometry.Get("location")
        Log(location)
    End If
    j.Release
End Sub
 

Attachments

  • GooglePlaceSearchBar.bas
    8.4 KB · Views: 340
Last edited:
Top