【问题标题】:Google place api call on AutoCompleteTextView in Appcompactivity - AndroidGoogle 在 Appcompactivity - Android 中对 AutoCompleteTextView 进行 api 调用
【发布时间】:2020-05-08 06:37:59
【问题描述】:

如何使用 Google 地点 API 在 EditText 中获得地点建议?

【问题讨论】:

  • 你知道如何从 Google place api 获取地点列表吗?

标签: java android google-maps android-studio google-places-api


【解决方案1】:

我建议你先通过谷歌的get started guide。然后在place autocomplete guide 之后将AutocompleteSupportFragment 添加到您的活动中。请看下面的代码:

// Initialize the SDK
Places.initialize(getApplicationContext(), "YOUR_API_KEY");

// Create a new Places client instance
PlacesClient placesClient = Places.createClient(this);

// Initialize the AutocompleteSupportFragment.
AutocompleteSupportFragment autocompleteFragment = (AutocompleteSupportFragment)
        getSupportFragmentManager().findFragmentById(R.id.autocomplete_fragment);

// Specify the types of place data to return.
autocompleteFragment.setPlaceFields(Arrays.asList(Place.Field.ID, Place.Field.NAME));

// Set up a PlaceSelectionListener to handle the response.
autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
    @Override
    public void onPlaceSelected(Place place) {
        // TODO: Get info about the selected place.
        Log.i(TAG, "Place: " + place.getName() + ", " + place.getId());
    }

    @Override
    public void onError(Status status) {
        // TODO: Handle the error.
        Log.i(TAG, "An error occurred: " + status);
    }
});

希望这可以帮助您入门!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-28
    • 1970-01-01
    • 1970-01-01
    • 2012-05-29
    • 1970-01-01
    • 1970-01-01
    • 2016-05-02
    相关资源
    最近更新 更多