- Android Google Places AutoComplete Feature-How to add it to your android app
- Access Google APIs
- Get started
- Access when authorization isn’t required
- Kotlin
- Access when authorization is required
- Kotlin
- Check for API availability
- Kotlin
- Overview
- Introduction
- Concepts
- API overview
- Policies and Terms
- Overview
- Introduction
- Concepts
- API overview
- Policies and Terms
- Custom Search JSON API
- Data format
- Related documents
- Prerequisites
- Search engine ID
- API key
- Pricing
- Monitoring
Android Google Places AutoComplete Feature-How to add it to your android app
In android development, searching for places around the globe has become easier and fun to implement with the Google Places SDK for Android that provides places autocomplete service.
“ The autocomplete service in the Places SDK for Android returns place predictions in response to user search queries. As the user types, the autocomplete service returns suggestions for places such as businesses, addresses and points of interest.”
In this post, we will explore the different ways we can add the autocomplete search feature to an android app. They are:
- Add as an autocomplete widget to save development time and ensure consistent user experience.
- Get place predictions programmatically to create a customized user experience.
NOTE: You’d need to create an API Key from the google cloud platform . You also need to register the app in the Google API Console so as to get an API KEY. Once, you have your API key, you can proceed… be sure to keep it safe.
By this time you should have created an android studio project for this.
Add the following dependency to your app’s build.gradle and sync project:
Copy the API key from the google console and add it to your strings.xml folder like this:
In the activity within which you want to implement the autocomplete search feature, you’d need to initialize Places like this:
Now we’re set to add the autocomplete search feature! We would explore both options.
Using this option, you can do either of the following:
a. Embed an AutocompleteSupportFragment or
b. Use an intent to launch the autocomplete activity.
Let’s consider option a 🙂
a. Embed an AutocompleteSupportFragment
This involves just adding a fragment to your activity’s XML layout and a listener to your activity or fragment.
Add the following code to your XML layout:
Next, initialize the AutocompleteSupportFragment and set the fields to specify which types of place data to return in the activity or fragment.
That’s it! you can run your code : )
b. Use an intent to launch the autocomplete activity.
Using this approach, I prefer to have a custom search icon on the toolbar which when clicked triggers the autocomplete search feature. I presume you already know how to add a search icon to the toolbar! You can add the following codes in your menu.xml file:
In your activity, after initializing Places, you can override the onOptionsItemSelected method which has the onSearchCalled() method when the search icon is clicked.
in the onSearchCalled() method, we set the fields to specify which types of place data to return. Thereafter, we use Autocomplete.IntentBuilder to create an intent, passing the desired PlaceAutocomplete mode (full-screen, or overlay). The intent must call startActivityForResult , passing in a request code that identifies your intent.
In my case, I specified 4 types of place data to return, you can specify more or less, be sure to check the documentation for other fields.
Also of note is the AUTOCOMPLETE_REQUEST_CODE which is a constant and can be any unique number.
Override the onActivityResult callback to receive the selected place.
That’s it! you can run your code : )
2. Get place predictions programmatically to create a customized user experience.
Your app can get a list of predicted place names and/or addresses from the autocomplete API by calling PlacesClient.findAutocompletePredictions() , passing a FindAutocompletePredictionsRequest object with the following parameters:
. a query string containing the text typed by the user ( Required)
. an AutocompleteSessionToken , which groups the query and selection phases of a user search into a discrete session for billing purposes. The session begins when the user starts typing a query and concludes when they select a place. ( Recommended)
. a RectangularBounds object, which specifies latitude and longitude bounds to constrain results to the specified region ( Recommended)
. an optional two-letter country code and a TypeFilter , which you can use to restrict the results to the specified place type.
Let’s add an EditText that would hold the text or query string from the user, a Button that would trigger the autocomplete search feature and a text area that would display the result. The resulting XML layout is as follows:
My approach here is that, once a user enters a search text in the EditText field, and clicks the button, the Places autocomplete search feature is triggered and the result is returned to the UI text area.
Note that the search result may be more than one.
Источник
Access Google APIs
When you want to make a call to one of the APIs in an SDK that’s powered by Google Play services, such as Google Sign-in or ML Kit, you need to first create an instance of an API client object. These objects automatically manage the connection to Google Play services. When a connection is available, each API client object executes requests in order. Otherwise, the client object queues the requests. Unless documentation indicates otherwise, client objects are cheap to construct; it’s fine to make new API clients every time you want to invoke API methods.
This guide shows how you can make API calls to any of the SDKs that are powered by Google Play services, including how to access the services that don’t require authorization and those that require authorization.
Get started
To get started, add the necessary tools and dependencies in your app project, as described in the guide on how to set up Google Play services.
Access when authorization isn’t required
To access a service that doesn’t require API authorization, get an instance of the service’s client object, passing it either the current Context or the current Activity . Before any API calls are executed, users are prompted to upgrade Google Play services if necessary.
For example, to get the device’s last known location using the Fused Location Provider for Android, add the logic that’s shown in the following code snippet:
Kotlin
Access when authorization is required
To access a service that requires user authorization, complete the following steps:
- Sign the user in.
- Request permission to access the scopes that the service requires.
- Get an instance of the service’s client object, passing it the user’s GoogleSignInAccount object in addition to a Context or Activity object.
The following example implements reading a user’s daily steps using the Google Fit API. To view a similar implementation in the context of a full project, view the main activity of the BasicHistoryApiKotlin app on GitHub.
Kotlin
Check for API availability
Before you enable a feature in your app that depends on a Google Play services API, include a check for the availability of the API on the device. To do so, call checkApiAvailability() .
The following code snippet demonstrates how to check for the availability of the fused location provider.
Kotlin
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Источник
Overview
Introduction
The Places SDK for Android allows you to build location-aware apps that respond contextually to the local businesses and other places near the user’s device. This means you can build rich apps based on places that mean something to the user, to complement the straightforward geographic-based services offered by the Android location services.
Concepts
The following interfaces provide the primary entry points to the Places SDK for Android:
- Places provides programmatic access to Google’s database of local place and business information, as well as the device’s current place.
- Autocomplete provides pre-made widgets to return place predictions in response to user search queries.
A place is defined as a physical space that has a name. Another way of thinking about a place is that it’s anything you can find on a map. Examples include local businesses, points of interest, and geographic locations. In the API, a place is represented by the Place interface. It includes information such as the name of the place and its address, geographical location, place ID, phone number, place type, website URL, and more.
API overview
Help your customers explore where they are and what’s around them:
- Place Autocomplete automatically fills in the name and/or address of a place as users type.
- Current Place returns a list of places where the user’s device is last known to be located along with an indication of the relative likelihood for each place.
- Place Details return and display more detailed information about a place.
- Place Photos returns high-quality images of a place.
- Place IDs stores the unique ID for one or more places for retrieval of place information on demand.
Note: Google gathers anonymous usage statistics.
Policies and Terms
All applications that use the Places SDK for Android must adhere to the requirements described in the Google Maps Platform Terms of Service, Usage and Billing, and Displaying Attributions.
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Источник
Overview
Introduction
The Places SDK for Android allows you to build location-aware apps that respond contextually to the local businesses and other places near the user’s device. This means you can build rich apps based on places that mean something to the user, to complement the straightforward geographic-based services offered by the Android location services.
Concepts
The following interfaces provide the primary entry points to the Places SDK for Android:
- Places provides programmatic access to Google’s database of local place and business information, as well as the device’s current place.
- Autocomplete provides pre-made widgets to return place predictions in response to user search queries.
A place is defined as a physical space that has a name. Another way of thinking about a place is that it’s anything you can find on a map. Examples include local businesses, points of interest, and geographic locations. In the API, a place is represented by the Place interface. It includes information such as the name of the place and its address, geographical location, place ID, phone number, place type, website URL, and more.
API overview
Help your customers explore where they are and what’s around them:
- Place Autocomplete automatically fills in the name and/or address of a place as users type.
- Current Place returns a list of places where the user’s device is last known to be located along with an indication of the relative likelihood for each place.
- Place Details return and display more detailed information about a place.
- Place Photos returns high-quality images of a place.
- Place IDs stores the unique ID for one or more places for retrieval of place information on demand.
Note: Google gathers anonymous usage statistics.
Policies and Terms
All applications that use the Places SDK for Android must adhere to the requirements described in the Google Maps Platform Terms of Service, Usage and Billing, and Displaying Attributions.
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Источник
Custom Search JSON API
The Custom Search JSON API lets you develop websites and applications to retrieve and display search results from Programmable Search Engine programmatically. With this API, you can use RESTful requests to get either web search or image search results in JSON format.
Data format
Custom Search JSON API can return results in JSON data format.
Related documents
The Custom Search JSON API uses the OpenSearch 1.1 Specification.
Prerequisites
Search engine ID
Before using the Custom Search JSON API you will first need to create and configure your Programmable Search Engine. If you have not already created a Programmable Search Engine, you can start by visiting the Programmable Search Engine control panel.
Follow the tutorial to learn more about different configuration options.
After you have created a Programmable Search Engine, visit the help center to learn how to locate your Search engine ID.
API key
Custom Search JSON API requires the use of an API key. Get a Key
Pricing
Custom Search JSON API provides 100 search queries per day for free. If you need more, you may sign up for billing in the API Console. Additional requests cost $5 per 1000 queries, up to 10k queries per day.
If you need more than 10k queries per day and your Programmable Search Engine searches 10 sites or fewer, you may be interested in the Custom Search Site Restricted JSON API, which does not have a daily query limit.
Monitoring
Basic monitoring for the Custom Search JSON API is available through Cloud Platform Console’s API Dashboard. For more advanced monitoring Google Cloud’s Operations suite (formerly Stackdriver) is available.
With Google Cloud Operations you can create custom dashboards, set up alerts, and access metrics data programmatically. To access Custom Search JSON API usage data in Google Cloud Operations, select «Resource type: Consumed API» and filter on «service = ‘customsearch.googleapis.com'» in the Query Builder.
See Monitoring Your API Usage for a discussion of the different monitoring and alerting capabilities provided by the API Dashboard and the Google Cloud Operations suite.
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Источник