如何在没有地图的情况下通过搜索获取​​地址列表

时间:2018-12-30 11:19:00

标签: java android google-maps

我正在尝试通过使用Google Maps API在editText中搜索地址来获取地址列表。

例如,如果我在屏幕顶部及其下方的editText中搜索地址,则会有一个地址列表供您选择。

(我不需要地图的一部分,只需要结果和搜索框)。

我一直在寻找它,似乎找不到它。

如果有人可以在这里帮助我,我会喜欢的。 谢谢

2 个答案:

答案 0 :(得分:1)

可以选择使用“ PlaceAutocompleteFragment”(当我第一次添加此帐户时,我没有帐单帐户,因此您应该不错)

1)将其添加到xml

<fragment
android:id="@+id/place_autocomplete_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment"
/>

2)使用PlaceSelectionListener

PlaceAutocompleteFragment autocompleteFragment = (PlaceAutocompleteFragment)
getFragmentManager().findFragmentById(R.id.place_autocomplete_fragment);

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

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

documentation 中的注释:

  

“ Google Places API需要API级别12或更高版本才能支持PlaceAutocompleteFragment对象。如果您定位的应用程序早于API级别12,则可以通过SupportPlaceAutocompleteFragment类访问相同的功能。还必须包括Android v4支持库。”

答案 1 :(得分:0)

首先,您需要一个组件,例如AutoCompleteTextView

第二Google拥有有关自动建议地址的文档。非常简单,注册,创建api密钥等。 Here is a link to doc.

逻辑:用户按输入文本,组件触发回调onTextChanged,并向用户显示收到的列表后,您请求获取建议列表。

相关问题