Android Tutorial Google Place

Discussion in 'Tutorials & Examples' started by BarryW, Apr 17, 2015.

  1. BarryW

    BarryW Active Member Licensed User

    This is a simple tutorial on how to search a place using Google Place API and with you keyword.
    Hope this can help for those who are confused... Like ME :):):)

    First you need an API Key(Browser Key) that is describe in Erel's Google Map v2

    Note: I used a browser key because it can be test in our browser on computer also it can be used to android dev.
    Ex: in Sydney&key=AddYourOwnKeyHere

    Other request type can be found here:

    Then Parse the result using JSON Tree

    Addition Libraries:

    This is my code:
    Sub Process_Globals
    Dim ApiKey As String = "AIzaxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" 'Add your browser key here.
    End Sub

    Sub Globals
    Dim Button1 As Button, EditText1 As EditText, CustomListView1 As CustomListView
    Dim PlaceList As List 'Request result list.
    End Sub

    Sub Activity_Create(FirstTime As Boolean)
        Button1.Background = LoadDrawable(
        CustomListView1.DefaultTextColor = 
    End Sub

    Sub Activity_Resume
    End Sub

    Sub Activity_Pause (UserClosed As Boolean)
    End Sub

    Sub Button1_Click
    Dim KeyWord As String = EditText1.Text.Trim
    If KeyWord = "" Then Return 'Exit when keyword is blank.
        KeyWord = KeyWord.Replace(" ""+"'Required to replace all space by + to use in request query.
    End Sub

    Sub Text_Search_Request(KeyWord As String)
    Dim job As HttpJob
    "SearchPlace", Me)
    "" & KeyWord & "&key=" & ApiKey)
    End Sub

    Sub JobDone (Job As HttpJob)
    Log("JobName = " & Job.JobName & ", Success = " & Job.Success)
    If Job.Success = True Then
    Select Job.JobName
    Case "SearchPlace"
    End Select
    Log("Error: " & Job.ErrorMessage)
    ToastMessageShow("Error: " & Job.ErrorMessage, True)
    End If
    End Sub

    Sub JsonTree(JobString As String)
    Dim parser As JSONParser
    Dim root As Map = parser.NextObject
    Dim results As List = root.Get("results")
    For Each colresults As Map In results
    Dim TempMap As Map : TempMap.Initialize
    Dim name As String = colresults.Get("name")
    "Name", name)
    Dim formatted_address As String = colresults.Get("formatted_address")
    "Address", formatted_address)
    Dim types As List = colresults.Get("types")
    Dim TempType As String = ""
    For Each coltypes As String In types
                TempType = TempType & coltypes & 
    " "
    "Type", TempType)
    Dim geometry As Map = colresults.Get("geometry")
    Dim Location As Map = geometry.Get("location")
    Dim lng As Double = Location.Get("lng")
    Dim lat As Double = Location.Get("lat")
    "Lat", lat)
    "Lng", lng)
    'Display results to customlistview.
    End Sub

    Sub Display_Result
    For x = 0 To PlaceList.Size - 1
    Dim TempMap As Map = PlaceList.Get(x)
    Dim SB As StringBuilder
    "Name: ").Append(TempMap.Get("Name"))
    "Address: ").Append(TempMap.Get("Address"))
    "Type: ").Append(TempMap.Get("Type"))
    "Latitute: ").Append(TempMap.Get("Lat"))
    "Longhitute: ").Append(TempMap.Get("Lng"))
            CustomListView1.AddTextItem(SB.ToString, x)
    End Sub

    Sub LoadDrawable(Name As StringAs Object
    'Gets a drawable from the Android system resources
        Dim R As Reflector
        R.Target = R.GetContext
        R.Target = R.RunMethod(
        R.Target = R.RunMethod(
    Dim ID_Drawable As Int
        ID_Drawable = R.RunMethod4(
    "getIdentifier"Array As Object(Name, "drawable""android"), _
    Array As String("java.lang.String""java.lang.String""java.lang.String"))
        R.Target = R.GetContext
        R.Target = R.RunMethod(
    Return R.RunMethod2("getDrawable", ID_Drawable, "")
    End Sub
    Also thanks to warwound (CustomListView)

    Attached Files:

  2. lemonisdead

    lemonisdead Well-Known Member Licensed User

    Nice tutorial. Perhaps should you urlEncode the query using StringUtils for more security
  3. BarryW

    BarryW Active Member Licensed User

    My url is not visible when users use the compiled apk so no need to encode it. Anyway thank you for your good suggestion...
  4. shashkiranr

    shashkiranr Active Member Licensed User

    Hi BarryW,

    Thank you for this tutorial. I have loaded my API key obtained in the the google maps tutorial. I am able to use that to locate places using lat and long but I am not able to retrieve the places using the above tutorial. When i checked the unfiltered logs i got the following error
    Couldn't connect to Google API client: ConnectionResult{statusCode=API_UNAVAILABLE, resolution=null}
    Any idea how to resolve this?

  5. shashkiranr

    shashkiranr Active Member Licensed User

    Hi I Got it working. I had put the SHA1 along with my package name in the browser key Referrers. I kept it blank and it is working. SO my question is what do you give in the Referrers for the browser key. ? For andriod we give the SHA1 key along with package name but for this its working only if it empty !

    Any suggestion?

  6. BarryW

    BarryW Active Member Licensed User

    Just leave it blank. Make sure you use browser key for google place not an android key.
  7. Reids

    Reids Member Licensed User

    enable this API not normal GOOGLE MAP API and then put the key inside the b4a api key section

    Attached Files:

    Filippo likes this.
  8. henry montoya

    henry montoya Member Licensed User


    excellent contribution I've tried it and I liked it a lot, but I wonder if it is possible as autocomplete and not as search, I know this thread has been a while so thanks for all the possible collaboration.
  9. Matias Ferrer

    Matias Ferrer Member Licensed User

    Hey there,
    I have a problem when compiling.

    He throws me the following error:


    B4A line: 53
    Log ( Response.GetString ( \
    javac 1.8.0_60
    src \ com \ domain \ appname \ : 163 : error : can not access ParseException
    anywheresoftware.b4a.keywords.Common.Log ( _response.GetString ( " UTF8 "));
    org.apache.http.ParseException file for class not found

    If I delete the log line (line 53 )

    I give this another mistake ..

    Compiling Java code generated . Error
    B4A line: 16
    hc.Initialize ( \
    javac 1.8.0_60
    src \ com \ domain \ appname \ : 223 : error : can not access ClientProtocolException
    _hc.Initialize ( " HC" ) ;
    org.apache.http.client.ClientProtocolException file for class not found

    I suggest something ?

    Thank you!
  10. BarryW

    BarryW Active Member Licensed User

    Try to update ur class or ur libraries...
    ihabsharaf and Matias Ferrer like this.
  11. Matias Ferrer

    Matias Ferrer Member Licensed User


    Were right , replace http by okhttp the library with their classes and it worked !

    Thank you!
    Gunnar Daehling likes this.
  12. Mashiane

    Mashiane Expert Licensed User

    Perfect, thank you so so so much...
  13. Filippo

    Filippo Expert Licensed User

    Thanks for this tutorial. :)
  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