如何使Google地方信息自动填充API特定于2个所需城市?

时间:2019-06-11 12:52:48

标签: android google-places-api

我正在学习根据Places SDK的新方法使用Google Places Autocomplete API,而不是现已弃用的先前的Places API,但我在全世界范围内都取得了这一成果。我希望结果特定于同一国家的两个理想城市。我怎样才能做到这一点?

主要活动

public class SearchAndFilter extends AppCompatActivity {

    PlacesClient mPlacesClient;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_and_filter);

        String apikey = "API_KEY";

        if(!Places.isInitialized()){
            Places.initialize(getApplicationContext(),apikey);
        }

        mPlacesClient = Places.createClient(this);

        final AutocompleteSupportFragment autocompleteSupportFragment =
                (AutocompleteSupportFragment) getSupportFragmentManager().findFragmentById(R.id.autocomplete_fragment);

        autocompleteSupportFragment.setPlaceFields(Arrays.asList(Place.Field.ID,Place.Field.LAT_LNG, Place.Field.NAME));
        autocompleteSupportFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
            @Override
            public void onPlaceSelected(@NonNull Place place) {
                final LatLng latLng = place.getLatLng();

                Toast.makeText(SearchAndFilter.this, ""+latLng.latitude+latLng.longitude, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onError(@NonNull Status status) {

            }
        });
    }
}

布局

<RelativeLayout 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"
    tools:context=".activity.SearchAndFilter">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:orientation="horizontal"
        android:layout_margin="20dp"
        android:background="@drawable/search_screen_bar_bg">

        <fragment
            android:name="com.google.android.libraries.places.widget.AutocompleteSupportFragment"
            android:id="@+id/autocomplete_fragment"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

    </LinearLayout>

</RelativeLayout>

如何使其针对同一国家的两个城市?

0 个答案:

没有答案