Giter VIP home page Giter VIP logo

drabu / placeautocomplete Goto Github PK

View Code? Open in Web Editor NEW
26.0 2.0 6.0 8.32 MB

A Kotlin library that helps developers save weeks of effort in migrating from the default Google Places Autocomplete feature.It uses the latest Places API and is build on top of MVVM Architecture and uses various RxJava operators to optimise location requests.

License: MIT License

Kotlin 97.75% Java 2.25%
placeautocompletefragment locationapi searchlocation places google-places place-autocomplete

placeautocomplete's Introduction

PlaceAutocomplete

Google Places Api Implementation

Download Codacy Badge License: MIT Android Arsenal

After Google came up with the update that Play Services version of the Places SDK for Android (in Google Play Services 16.0.0) is deprecated as of January 29, 2019, and will be turned off on July 29, 2019. which includes PlaceAutocompleteFragment also to be removed from the library.After getting this update from google we had to immediately migrate to an alternative where we ended up creating this library, PlaceAutocompleteFragment provides you with an elegant user interface to choose a location and works the exact same as PlaceAutocompleteFragment .

Features

Custom title message for the view.

Sorted places based on your location provided you pass your location.

Radius of enclosure for search

Support for activity animation for a smooth transition

Support for landscape orientation.

This Kotlin Library is build with MVVM Archetecture and runs on top of RxJava 2. The library uses various Rx operators like debounce, throttlefirst operators to reduce network calls and optimze location query.

What's New

#1 Two different loaders attached, one for getting the list of predicted items and one for getting place details.
#2 Erase query with a close icon added to clear the text in the field.
#3 Keeps a record of Recent searches (Be Sure to add the dependency).
#4 Maintains recent searches in pretty time groups like Today, Yesterday, Earlier this week and previous searches.
#5 Internet connectivity and error handling also added wherever needed

Dependencies

Since this library is build with MVVM Architecture and uses Kotlin, RxAndroid, RxJava 2, Rx Binding,recyclerview, cardview, retrofit and various android architecture components, so you can find the dependencies here. and add them to your build.gradle file.

How it works

Demonstartion image.

Configuration

Add the dependency:

dependencies {
		//copy the version from download badge above 	
	implementation 'com.opensource.oneclickaway.android.searchplaces:place_autocomplete:x.x.x'
 }

Mandatory and optional parameters :

val intent = Intent(this, SearchPlaceActivity::class.java)
    intent.putExtra(
        SearchPlacesStatusCodes.CONFIG,
        SearchPlaceActivity.Config.Builder(apiKey = "PASS YOUR API KEY HERE")
            .setSearchBarTitle("Enter Source Location")
            .setMyLocation("12.9716,77.5946")
            .setEnclosingRadius("500")
            .build()
    )

For build version greater LOLLIPOP, you can use Activity Transition like this:

val pair = Pair.create(searchLocationET as View, SearchPlacesStatusCodes.PLACEHOLDER_TRANSITION)
val options = ActivityOptions.makeSceneTransitionAnimation(this, pair).toBundle()
startActivityForResult(intent, 700, options)

#Example Kotlin Class:

import com.oneclickaway.opensource.placeautocomplete.data.api.bean.place_details.PlaceDetails
import com.oneclickaway.opensource.placeautocomplete.components.SearchPlacesStatusCodes
import com.oneclickaway.opensource.placeautocomplete.ui.SearchPlaceActivity

class ExampleLocationSearch : AppCompatActivity() {

 lateinit var searchLocationET: EditText
 lateinit var placeDetailsTV: TextView
 var REQUEST_CODE = 700

 var API_KEY = BuildConfig.ApiKey

 override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_example_location_search)

    searchLocationET = findViewById(R.id.searchLocationBTN)
    placeDetailsTV = findViewById(R.id.resultPlaceDetailsTV)

 val intent = Intent(this, SearchPlaceActivity::class.java)
	intent.putExtra(
	    SearchPlacesStatusCodes.CONFIG,
	    SearchPlaceActivity.Config.Builder(apiKey = API_KEY)
		.setSearchBarTitle("Enter Source Location")
		.setMyLocation("12.9716,77.5946")
		.setEnclosingRadius("500")
		.build()
	)

    searchLocationET.setOnClickListener {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            val pair = Pair.create(searchLocationET as View, SearchPlacesStatusCodes.PLACEHOLDER_TRANSITION)
            val options = ActivityOptions.makeSceneTransitionAnimation(this, pair).toBundle()
            startActivityForResult(intent, REQUEST_CODE, options)

        } else {
            startActivityForResult(intent, REQUEST_CODE)
            overridePendingTransition(R.anim.abc_fade_in, R.anim.abc_fade_out)
        }


    }


}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)

    if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK) {

        val placeDetails = data?.getParcelableExtra<PlaceDetails>(SearchPlacesStatusCodes.PLACE_DATA)

        searchLocationET.setText(placeDetails?.name)

        placeDetailsTV.text = placeDetails.toString()
        Log.i(javaClass.simpleName, "onActivityResult: ${placeDetails}  ")
    }
}
}

Usage

-Minimum sdk 15.
-Returns place information in onActivityResult

License

MIT License

Copyright (c) 2019 Burhan ud din

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

placeautocomplete's People

Contributors

drabu avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

placeautocomplete's Issues

Wrong position when clicked in Adapter

Test case :
1, Input search : "Ha"
2, loading adapter complete
3, add : "Noi " -> edittext : "Ha Noi"
4, loading adapter complete
5, when i choose one item , the result will be wrong place

 override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.binding.placeTitleTV.text =
            listOfCandidatesItem?.get(position)?.structuredFormatting?.mainText
        holder.binding.placeFormattedAddressTV.text =
            listOfCandidatesItem?.get(position)?.structuredFormatting?.secondaryText
        Log.e("HVV1312Json", "AAA ${listOfCandidatesItem!![position]?.terms?.get(0)?.value}")

        RxView.clicks(holder.itemView)
            .throttleFirst(700, TimeUnit.MILLISECONDS)
            .subscribe {
                placeClickListerner.onPlaceClicked(listOfCandidatesItem?.get(holder.adapterPosition))
            }

    }

if i do this , => the result correct

Crash in StructuredFormatting.getMainText

Thanks for the library, it saved a lot of time implementing similar. I've just deployed a release with this library in and I'm seeing the following crash (currently Android 8 and 9 but it's early days so that could be irrelevant).

Fatal Exception: java.lang.NullPointerException: throw with null exception
       at com.oneclickaway.opensource.placeautocomplete.api.bean.places_response.StructuredFormatting.getMainText(StructuredFormatting.java:3)
       at com.oneclickaway.opensource.placeautocomplete.ui.SearchResultAdapter.onBindViewHolder(SearchResultAdapter.java:31)
       at com.oneclickaway.opensource.placeautocomplete.ui.SearchResultAdapter.onBindViewHolder(SearchResultAdapter.java:16)
       at androidx.recyclerview.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6781)
       at androidx.recyclerview.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6823)

Unresolved reference: Builder

I am getting this error:
Unresolved reference: Builder

builder

Please inform us how to fix it, thank you

Here's my full code:

import android.app.ActivityOptions
import android.content.Intent
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.EditText
import android.widget.TextView
import com.oneclickaway.opensource.placeautocomplete.components.SearchPlacesStatusCodes
import com.oneclickaway.opensource.placeautocomplete.ui.SearchPlaceActivity

import android.util.Pair

class MainActivity : AppCompatActivity() {

lateinit var searchLocationET: EditText
lateinit var placeDetailsTV: TextView
var REQUEST_CODE = 700

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    searchLocationET = findViewById(R.id.searchLocationBTN)
    placeDetailsTV = findViewById(R.id.resultPlaceDetailsTV)

    val intent = Intent(this, SearchPlaceActivity::class.java)
    intent.putExtra(
        SearchPlacesStatusCodes.CONFIG,
        SearchPlaceActivity.Config.Builder(apiKey = "myapikey")
            .setSearchBarTitle("Enter Source Location")
            .setMyLocation("12.9716,77.5946")
            .setEnclosingRadius("500")
            .build()
    )

    searchLocationET.setOnClickListener {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            val pair = Pair.create(searchLocationET as View, SearchPlacesStatusCodes.PLACEHOLDER_TRANSITION)
            val options = ActivityOptions.makeSceneTransitionAnimation(this, pair).toBundle()
            startActivityForResult(intent, REQUEST_CODE, options)

        } else {
            startActivityForResult(intent, REQUEST_CODE)
            overridePendingTransition(R.anim.abc_fade_in, R.anim.abc_fade_out)
        }
    }
}

}

Recent searches improvement

Hi, I like your library it's easy to use and very straight forward but I noticed that you're calling the details API again when you click the item on the recent searches, why do you need to do that? you already saved the details in your local DB, we can save the daily usage of the API if we don't need to query again. Thank you! just a thought. :)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.