如何在没有地图的情况下通过搜索获取地址列表?
我正在尝试通过使用Google Maps API在editText
中搜索地址来获取地址列表.
I'm trying to get a list of addresses by searching an address in an editText
, using Google Maps API.
例如,如果我要在屏幕顶部的editText中搜索地址,则该地址下方将显示一个可供选择的地址列表.
For example, if I'm searching for an address in the editText at the top of the screen, below it, there will be a list of addresses that you can choose.
我不需要地图的一部分,只需要结果和搜索框.
I don't need the map part of it, just the results, and the search box.
可以选择使用PlaceAutocompleteFragment
-当我第一次添加它时,我没有计费帐户,因此您应该会很好:
There is the option of using the PlaceAutocompleteFragment
- when I first added it, I had no billing account so you should be good:
-
将其添加到XML
Add it on the 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"
/>
使用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);
}
});
文档 中的注释:
Note from the documentation:
Google Places API需要API级别12或更高级别才能支持
PlaceAutocompleteFragment
对象.如果您要定位API级别12之前的应用程序,则可以通过SupportPlaceAutocompleteFragment
类访问相同的功能.您还必须包括Android v4支持库.
The Google Places API requires API level 12 or higher for the support of
PlaceAutocompleteFragment
objects. If you are targeting an application earlier than API level 12, you can access the same functionality through theSupportPlaceAutocompleteFragment
class. You must also include the Android v4 Support Library.