Skip to content

SiteAutocompleteFragment for site (place) search suggestions using hms site kit, alternative to Google places AutocomopleteSupportFragment

License

Notifications You must be signed in to change notification settings

megaacheyounes/siteautocomoplete

Repository files navigation

API uses gms uses hms

An AutocompleteSupportFragment for HMS Site kit

This library provides a fragment similar to the Google Places AutocompleteSupportFragment, for the HMS Site kit Shows suggestions when searching for places

Configuration

WARNING: Before you can start using the HMS Platform APIs and SDKs, you must register your app in the AppGallery and configure your project, this step cannot be skipped.

See configuration Instructions.

Installation

add jitpack repo to your project level build.gradle file:

        allprojects {
    		repositories {
    			maven { url 'https://jitpack.io' }
    		}
    	}

add the library to your app level 'build.bradle' file:

	dependencies {
	        implementation 'com.github.megaacheyounes:siteautocomoplete:1.0.5'
	}

Add the fragment

see MainActivity and MainActivityJava for complete example

Method 1 (xml):

add the fragment tag with the name attribute:

    <fragment
        android:id="@+id/fragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:name="com.megaache.siteautocomplete.SiteAutocompleteFragment">

    </fragment>

Initialize the fragment:

    val autocompleteFragment = supportFragmentManager.findFragmentById(R.id.fragment) as SiteAutocompleteFragment

Method 2 (kotlin):

declare the fragment container:

   <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    </FrameLayout>

then add SiteAutocompleteFragment fragment dynamically using the supportFragmentManager:

   val autocompleteFragment = SiteAutocompleteFragment()

   supportFragmentManager.beginTransaction().replace(R.id.fragment_container, fragment)
            .commit()

   supportFragmentManager.executePendingTransactions()

Display mode

  • Fullscreen mode:

fullscreen mode

            autocompleteFragment.setActivityMode(SiteAutocompleteMode.FULLSCREEN)
  • Overlay mode:

overlay mode

            autocompleteFragment.setActivityMode(SiteAutocompleteMode.OVERLAY)

Listen for Place selection event

          autocompleteFragment.setOnSiteSelectListener(
                object : SiteSelectionListener {
                    override fun onSiteSelected(site: Site) {
                        Log.d("onSiteSelected", site.name)
                    }
                    override fun onError(error: Error) {
                        Log.d("onError", error.toString())
                    }
                }
            )

show selected site on search view:

if you call this method, when the user select a site (place), the name and the address of that place will replace the placeholder "search sites..." in search view (autocomplete EditText ), if false passed then only the name of the selected site will be shown

         autocompleteFragment.showSelectedSiteOnSearchView(true)

Internationalization

The fragment supports 8 languages: English, Arabic, Chinese, French, Hindi, German, Russian, Spanish

Important

To run the example app you must create an app in huawei console, then add the config file 'agconnect-config.json' into the project and update the package name according to package name in config file

Contributions

All kinds of contributions are welcome

HMS

learn more about HMS: here

  Copyright (C) 2020 Younes Megaache

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.

About

SiteAutocompleteFragment for site (place) search suggestions using hms site kit, alternative to Google places AutocomopleteSupportFragment

Resources

License

Stars

Watchers

Forks

Packages

No packages published