意图包含一个地方,但不包含

问题描述 投票:0回答:1

以相同的请求代码和结果状态调用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.

google-maps kotlin android-intent google-places-api onactivityresult
1个回答
0
投票
 /*-- 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.
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.