Setting Carmen Feature address to my Address Object (Mapbox Android)

▼魔方 西西 提交于 2021-01-07 03:22:59

问题


Basically I need to set this string address returned from CarmenFeature to my Address Object.

The below code basically returns the string address using the placeName method.

 private void reverseGeocode(final Point point) {
        try {
            MapboxGeocoding client = MapboxGeocoding.builder()
                    .accessToken(getString(R.string.access_token))
                    .query(Point.fromLngLat(point.longitude(), point.latitude()))
                    .geocodingTypes(GeocodingCriteria.TYPE_ADDRESS)
                    .build();

            client.enqueueCall(new Callback<GeocodingResponse>() {
                @Override
                public void onResponse(Call<GeocodingResponse> call, Response<GeocodingResponse> response) {

                    if (response.body() != null) {
                        List<CarmenFeature> results = response.body().features();
                        if (results.size() > 0) {
                            CarmenFeature feature = results.get(0);

// If the geocoder returns a result, we take the first in the list and show a Toast with the place name.
                            mapboxMap.getStyle(new Style.OnStyleLoaded() {
                                @Override
                                public void onStyleLoaded(@NonNull Style style) {
                                    if (style.getLayer(DROPPED_MARKER_LAYER_ID) != null) {
                                        Toast.makeText(SearchLocationActivity.this,
                                                String.format(getString(R.string.location_picker_place_name_result),
                                                        feature.placeName()), Toast.LENGTH_SHORT).show();
                                        Log.d(TAG, "Reverse geocode address : "+ feature.placeName().toString() );
                                        showAddress.setText(getValidAddress(address));
                                        address = convertStringToAddress(feature.placeName());

                                    }
                                }
                            });

                        } else {
                            Toast.makeText(SearchLocationActivity.this,
                                    getString(R.string.location_picker_dropped_marker_snippet_no_results), Toast.LENGTH_SHORT).show();
                        }
                    }
                }

My Address Object :

 private String line1;
    private String line2;
    private String city;
    private String state;
    private String zip;
    private String country;

Now is there any way I could get the address from CarmenFeature separately, in the sence rather than using placeName, is there any way I could get line1, line2 and state, zipcode etc separately so that I could set it to my Address Object.

For your information, that convertStringToAddress basically does this:

    private Address convertStringToAddress(String address){
        Address finalAddress = new Address();
        List<String> values = Arrays.asList(address.split(","));
        finalAddress.setLine1(values.get(0));
        finalAddress.setLine2(values.get(1));
        finalAddress.setCity(values.get(2));
        finalAddress.setState(values.get(3));
        finalAddress.setZip(values.get(4));
        finalAddress.setCountry(values.get(5));

        return finalAddress;

    }

But this won't work well if the address string is something like this :

740 15th Street NW, Washington DC

That's why I need a way to separately get the zipcode, address line 1 and 2 and state so that I can separately set the values to it.

来源:https://stackoverflow.com/questions/65139756/setting-carmen-feature-address-to-my-address-object-mapbox-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!