Предполагается, что намерение будет содержать место, но не содержит - PullRequest
1 голос
/ 07 ноября 2019

Использование AutoComplete.IntentBuilder() не возвращает место, когда вызывается onActivityResult() с тем же кодом запроса и статусом результата.

Я поиграл с полями мест, если это связано сничего, кроме неудачи.

class MainActivity : AppCompatActivity() {

    private var placeFields = listOf(
        Place.Field.ID,
        Place.Field.NAME,
        Place.Field.ADDRESS,
        Place.Field.LAT_LNG
    )

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        button.setOnClickListener {
            initPlaces()
            setupPlacesAutocomplete()
        }
    }

    private fun setupPlacesAutocomplete() {

        val autocompleteIntent = Autocomplete.IntentBuilder(
            AutocompleteActivityMode.FULLSCREEN,
            placeFields
        ).setTypeFilter(TypeFilter.CITIES).build(this)

        startActivityForResult(autocompleteIntent, 5)
    }

    private fun initPlaces() {
        Places.initialize(this, getString(R.string.places_api))
        val placesClient = Places.createClient(this)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)

        if (requestCode == 5) {
            if (resultCode == AutocompleteActivity.RESULT_OK) {
                val place = Autocomplete.getPlaceFromIntent(intent)
                txt.text = place.name
            }
        }
    }
}

Я ожидаю, что OnActivityResult () должен вернуть место, но вместо этого я получаю java.lang.IllegalArgumentException: Intent expected to contain a Place, but doesn't.

1 Ответ

1 голос
/ 07 ноября 2019
 /*-- function to open address search activity --**/
    public void createAutoCompleteIntent() {
        if (getActivity() != null) {
            List<Place.Field> fields = Arrays.asList(Place.Field.ID, Place.Field.NAME, Place.Field.LAT_LNG, Place.Field.ADDRESS);
            Intent intent = new Autocomplete.IntentBuilder(
                    AutocompleteActivityMode.FULLSCREEN, fields)
                    .build(getActivity());
            startActivityForResult(intent, AUTOCOMPLETE_REQUEST_CODE);
        }
    }

 @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == AUTOCOMPLETE_REQUEST_CODE) {
        if (resultCode == RESULT_OK) {
            Place place = Autocomplete.getPlaceFromIntent(data);
            if (place.getLatLng() != null) {
                // reverse geoCoding to get Street Address, city,state and postal code

                Geocoder geocoder = new Geocoder(getActivity(), Locale.getDefault());
                try {
                    System.out.println("------addressList-----" + place.getAddress() + "             " + place.getName());
                    List<Address> addressList = geocoder.getFromLocation(
                            place.getLatLng().latitude, place.getLatLng().longitude, 1);
                    System.out.println("------addressList-----" + addressList);
                    if (addressList != null && addressList.size() > 0) {
                        Address address = addressList.get(0);
                        System.out.println("------address-----" + address);
                        addressEd.setText(address.getAddressLine(0));
                        String featureName = "";
                        if (address.getFeatureName()!=null){
                            featureName = address.getFeatureName();
                        }
                        String throughFare = "";
                        if (address.getThoroughfare()!=null){
                            throughFare = address.getThoroughfare();
                        }
                        String streetAddress = featureName + " " + throughFare;
                        streetAddressEd.setText(streetAddress);
                        if (address.getLocality() != null) {
                            cityEd.setText(address.getLocality());
                        } else {
                            callGeoCodeAPI(place.getLatLng().latitude + "," + place.getLatLng().longitude);
                        }
                        stateEd.setText(address.getAdminArea());
                        postCodeEd.setText(address.getPostalCode());
                        countryEd.setText(address.getCountryName());
                    }

                } catch (IOException e) {
                    Log.e(TAG, "Unable connect to Geocoder", e);
                }

            }
        } else if (resultCode == AutocompleteActivity.RESULT_ERROR) {
            Status status = Autocomplete.getStatusFromIntent(data);
            if (getActivity() != null) {
                Util.showMessageBar(getActivity(), status.getStatusMessage());
            }
        } else if (resultCode == RESULT_CANCELED) {
            // The user canceled the operation.
        }
    }
}
...