agment和PlaceAutocompleteActivity

agment和PlaceAutocompleteActivity

本文介绍了如何实现PlaceAutocompleteFragment和PlaceAutocompleteActivity以获取Place详细信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我使用



PlaceAutocompleteActivity

  private void callPlaceAutocompleteActivityIntent(){
try {
意图意图=
新的PlaceAutocomplete.IntentBuilder(PlaceAutocomplete.MODE_FULLSCREEN)
。建立(本);
startActivityForResult(intent,PLACE_AUTOCOMPLETE_REQUEST_CODE);
// PLACE_AUTOCOMPLETE_REQUEST_CODE是请求代码的整数
} catch(GooglePlayServicesRepairedException | GooglePlayServicesNotAvailableException e){
// TODO:处理错误。



$ b @Override
protected void onActivityResult(int requestCode,int resultCode,Intent data){
super.onActivityResult( requestCode,resultCode,data);
//autocompleteFragment.onActivityResult(requestCode,resultCode,data);
if(requestCode == PLACE_AUTOCOMPLETE_REQUEST_CODE){
if(resultCode == RESULT_OK){
Place place = PlaceAutocomplete.getPlace(this,data);
Log.i(TAG,Place:+ place.toString());
} else if(resultCode == PlaceAutocomplete.RESULT_ERROR){
Status status = PlaceAutocomplete.getStatus(this,data);
Log.i(TAG,status.getStatusMessage());
} else if(resultCode == RESULT_CANCELED){



$ b code $ <$ pre >

输出:



希望它的帮助。

编辑:更改 requestCode == RESULT_CANCELED resultCode == RESULT_CANCELED


I am using Google Place to get place details. Google provide different way to implement Google Place API to get place details.Different way are like PlaceAutocompleteFragment , PlaceAutocompleteActivity . How differentiate these all and how to implement to get place details using Google place API.

解决方案

First of all need to API key and Enable Google Place API to search and get place details. Add your API key to your app manifest ,need to replacing YOUR_API_KEY with your own API key:

<application>
  ...
  <meta-data
      android:name="com.google.android.geo.API_KEY"
      android:value="YOUR_API_KEY"/>
</application>

1) PlaceAutocompleteFragment

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

Java:

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

/*
* The following code example shows setting an AutocompleteFilter on a PlaceAutocompleteFragment to
* set a filter returning only results with a precise address.
*/
AutocompleteFilter typeFilter = new AutocompleteFilter.Builder()
        .setTypeFilter(AutocompleteFilter.TYPE_FILTER_ADDRESS)
        .build();
autocompleteFragment.setFilter(typeFilter);

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

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

Output:

2) PlaceAutocompleteActivity

private void callPlaceAutocompleteActivityIntent() {
    try {
        Intent intent =
                new PlaceAutocomplete.IntentBuilder(PlaceAutocomplete.MODE_FULLSCREEN)
                        .build(this);
        startActivityForResult(intent, PLACE_AUTOCOMPLETE_REQUEST_CODE);
//PLACE_AUTOCOMPLETE_REQUEST_CODE is integer for request code
    } catch (GooglePlayServicesRepairableException | GooglePlayServicesNotAvailableException e) {
        // TODO: Handle the error.
    }

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    //autocompleteFragment.onActivityResult(requestCode, resultCode, data);
    if (requestCode == PLACE_AUTOCOMPLETE_REQUEST_CODE) {
        if (resultCode == RESULT_OK) {
            Place place = PlaceAutocomplete.getPlace(this, data);
            Log.i(TAG, "Place:" + place.toString());
        } else if (resultCode == PlaceAutocomplete.RESULT_ERROR) {
            Status status = PlaceAutocomplete.getStatus(this, data);
            Log.i(TAG, status.getStatusMessage());
        } else if (resultCode == RESULT_CANCELED) {

        }
    }
}

Output:

Hope its help.

Edit: change requestCode == RESULT_CANCELED to resultCode == RESULT_CANCELED

这篇关于如何实现PlaceAutocompleteFragment和PlaceAutocompleteActivity以获取Place详细信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 23:48