【问题标题】:How To Integrate Google Places Autocomplete In android After google places sdk is depreciated如何在 android 中集成 Google Places 自动完成功能 google places sdk 折旧后
【发布时间】:2019-08-21 07:18:30
【问题描述】:

我在 Google Play 控制台上为我的项目创建了一个 Api 密钥,并从 API 部分启用了 google Place API 并添加了一个 元标记如下:

 <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" />

在我的项目清单文件中。

这是我的 XML 文件:

<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>

这是我的 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.
        }
    }
}

}

但我没有得到我想要的结果,在这个项目中我多次收到 OVER_LIMIT_QUERY 错误,有时它说没有结果这是不可能的,我也没有在下拉列表中得到小地方或村庄的建议。

任何人都可以分享完整的指南或一些 Github 项目以在 Android 中集成 Google Place AutoComplete 以及我们需要在 Google Play 商店上对我们的项目进行哪些设置。

【问题讨论】:

标签: android android-studio google-places-api googleplacesautocomplete


【解决方案1】:

我还发现 Google 更改了地点搜索 API 的文档。您可以在此处查看place autocomplete 的完整文档。我已经建立了一个地方搜索库。你也可以使用它。请参阅此处simple place search

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-01
    • 2013-10-10
    • 2016-11-29
    • 1970-01-01
    相关资源
    最近更新 更多