Reputation: 187
I Created an Api key on Google play console for my project and Enabled the google Place API from API Section and added a metatag as below :
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_play_api" />
in my project's manifest file.
This is my XML File :
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="click me" /></LinearLayout>
This is my MainActivity :
package com.example.velmurugan.googleautocompleteplacesandroid;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.gms.common.api.Status;
import com.google.android.gms.location.places.AutocompleteFilter;
import com.google.android.gms.location.places.ui
.PlaceAutocompleteFragment;
import com.google.android.gms.location.places.ui.PlaceSelectionListener;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.libraries.places.api.Places;
import com.google.android.libraries.places.api.model.Place;
import com.google.android.libraries.places.api.model.RectangularBounds;
import com.google.android.libraries.places.api.model.TypeFilter;
import com.google.android.libraries.places.widget.Autocomplete;
import com.google.android.libraries.places.widget.AutocompleteActivity;
import com.google.android.libraries.places.widget.model
.AutocompleteActivityMode;
import java.util.Arrays;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private PlaceAutocompleteFragment placeAutocompleteFragment;
private final int AUTOCOMPLETE_REQUEST_CODE = 001;
Button click_me;
private final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Places.initialize(getApplicationContext(), "AIzaSyAnLeOitlBUxo7mE6L9lIFH80TbWO6sSNQ");
final List<Place.Field> fields = Arrays.asList(Place.Field.ID, Place.Field.NAME);
click_me = findViewById(R.id.btn);
click_me.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Autocomplete.IntentBuilder(
AutocompleteActivityMode.FULLSCREEN, fields).setCountry("IN").setTypeFilter(TypeFilter.ADDRESS)
.build(MainActivity.this);
startActivityForResult(intent, AUTOCOMPLETE_REQUEST_CODE);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == AUTOCOMPLETE_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
Place place = Autocomplete.getPlaceFromIntent(data);
Log.i(TAG, "Place: " + place.getName() + ", " + place.getId());
} else if (resultCode == AutocompleteActivity.RESULT_ERROR) {
// TODO: Handle the error.
Status status = Autocomplete.getStatusFromIntent(data);
Log.i(TAG, status.getStatusMessage());
} else if (resultCode == RESULT_CANCELED) {
// The user canceled the operation.
}
}
}
}
But I am not getting the result as i want ,In this project I am getting OVER_LIMIT_QUERY Error many times and sometimes it says no result Which is not possible also I am not getting small places or villages suggestion in the dropdown.
Can Anyone Please Share the complete Guide or some Github Project to Integrate Google Place AutoComplete in Android also what are the settings we need to do On Google Play store Regarding our Project .
Upvotes: 0
Views: 4364
Reputation: 875
I also find that Google has changed the documentation for the place search API. you can check here full documentation for place autocomplete. And I have made a library for place search. You can also use that. See here the simple place search.
Upvotes: 1