Kotlin Google Maps AutocompleteTextView Location Example – Coding Shiksha

Kotlin Google Maps AutocompleteTextView Location Example – Coding Shiksha

Welcome Folks I am back with another blog post. In this post we will be talking about a Kotlin Library which allows you to use a Kotlin AutoCompleteTextView Widget in Android Application. The demo of the Application is given below. All the source code of the application is given below.

 

 

 

 

Download Library

 

Download the library here

 

Add the Library in Application

 

In your Project build.gradle you must have the following

buildscript {
    ...
    repositories {
        ...
        jcenter()
    }
    ...
    dependencies {
        ...
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
    ...
}

 

READ  Kotlin Date time picker using JRDateTimePicker

In your App build.gradle you must have the following

apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
...
dependencies {
    ...
    implementation "com.jesualex.android:autocompletelocation:$last_version"
}

 

Get a Google Maps API Key and enabled the Google Places API for Android (Add your API Key in values):

<resources>
    <string name="google_places_key"
        translatable="false"
        templateMergeStrategy="preserve">YOUR_KEY_HERE</string>
  </resources>

 

Add the AutocompleteLocation into the layout:

<com.jesualex.autocompletelocation.AutocompleteLocation
      android:id="@+id/autocomplete_location"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"/>

 

Full Example Source Code

 

  1. Layout File:

 

activity_main.xml

 

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/parentLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.jesualex.autocompletelocation.sample.MainActivity">

  <fragment
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:id="@+id/map"
      android:name="com.google.android.gms.maps.SupportMapFragment"
      android:layout_alignParentTop="true"
      android:layout_alignParentStart="true" />

  <com.jesualex.autocompletelocation.AutocompleteLocation
      android:id="@+id/autocomplete_location"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_gravity="center_horizontal"
      android:layout_margin="12dp"
      app:background_layout="@drawable/bg_rounded_white"
      app:closeIcon="@drawable/ic_close"
      app:countryCode="au"/>
</RelativeLayout>

 

READ  Kotlin (Android) Result Progress Bar Design [NEW] - Coding Shiksha

2) Kotlin Code

 

MainActivity.kt

 

package com.jesualex.autocompletelocation.sample

import android.os.Bundle
import android.support.v4.app.FragmentActivity
import android.util.Log

import com.google.android.gms.maps.CameraUpdateFactory
import com.google.android.gms.maps.GoogleMap
import com.google.android.gms.maps.OnMapReadyCallback
import com.google.android.gms.maps.SupportMapFragment
import com.google.android.gms.maps.model.LatLng
import com.google.android.gms.maps.model.MarkerOptions
import com.google.android.libraries.places.api.model.AutocompletePrediction
import com.google.android.libraries.places.api.model.Place
import com.google.android.libraries.places.api.model.RectangularBounds
import com.jesualex.autocompletelocation.AutocompleteLocation
import com.jesualex.autocompletelocation.AutocompleteLocationListener
import com.jesualex.autocompletelocation.OnPlaceLoadListener
import com.jesualex.autocompletelocation.OnSearchListener
import com.jesualex.autocompletelocation.PlaceUtils

class MainActivity : FragmentActivity(),
        OnMapReadyCallback,
        AutocompleteLocationListener,
        OnSearchListener,
        OnPlaceLoadListener
{
    private var mMap: GoogleMap? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val mapFragment = supportFragmentManager.findFragmentById(R.id.map) as SupportMapFragment
        mapFragment.getMapAsync(this)

        val bounds = RectangularBounds.newInstance(
                LatLng(-33.880490, 151.184363),
                LatLng(-33.858754, 151.229596))

        val autoCompleteLocation = findViewById<AutocompleteLocation>(R.id.autocomplete_location)
        autoCompleteLocation.setAutoCompleteTextListener(this)
        autoCompleteLocation.setOnSearchListener(this)
        autoCompleteLocation.setCountry("Au")
        autoCompleteLocation.setLocationBias(bounds)

        //Set placeListener to auto calculate Place object when a AutocompletePrediction has selected.
        autoCompleteLocation.setPlaceListener(this)

        //Set placeFields to receive only the fields what you need. By default PlaceUtil.getDefaultFields() is call.
        autoCompleteLocation.setPlaceFields(PlaceUtils.defaultFields)
    }

    override fun onMapReady(googleMap: GoogleMap) {
        mMap = googleMap

        val madrid = LatLng(40.4167754, -3.7037902)
        mMap!!.moveCamera(CameraUpdateFactory.newLatLngZoom(madrid, 16f))
    }

    override fun onTextClear() {
        mMap!!.clear()
    }

    override fun onItemSelected(selectedPlace: AutocompletePrediction?) {
        Log.i(javaClass.simpleName, "A autocomplete has selected: ")
        selectedPlace?.let { logAutocomplete(it) }
    }

    override fun onSearch(address: String, predictions: List<AutocompletePrediction>) {
        for (prediction in predictions) {
            logAutocomplete(prediction)
        }
    }

    override fun onPlaceLoad(place: Place) {
        addMapMarker(place.latLng)
    }

    private fun logAutocomplete(selectedPrediction: AutocompletePrediction) {
        Log.i(javaClass.simpleName, selectedPrediction.placeId)
        Log.i(javaClass.simpleName, selectedPrediction.getPrimaryText(null).toString())
    }

    private fun addMapMarker(latLng: LatLng?) {
        mMap!!.clear()
        mMap!!.addMarker(MarkerOptions().position(latLng!!))
        mMap!!.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 16f))
    }
}

Leave a Reply

Close Menu