如何从地点选择器的地址获取国家,城市? [英] How to get country, city from place picker's address?

查看:181
本文介绍了如何从地点选择器的地址获取国家,城市?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用地点选择器的意图来获取地点.现在,我想以单独的形式将地址保存为国家,城市,密码,州.如何从地点选择器的地址获得所有这些信息?

I am using a place picker's intent to get the place. Now I want to save address in separated form as country,city,pincode,state. How can I get all this from the place picker's address?

代码:

public class NameOfBusinessFragment extends Fragment {

    int PLACE_PICKER_REQUEST = 1;
    int RESULT_OK = -1;
    PlacePicker.IntentBuilder builder = new PlacePicker.IntentBuilder();
    EditText category,location;

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.fragment_name_of_business,
                    container, false);

            location = (EditText)view.findViewById(R.id.location);

            location.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    try {

                        startActivityForResult(builder.build(getActivity()), PLACE_PICKER_REQUEST);
                    }
                    catch (GooglePlayServicesRepairableException e)
                    {
                        Toast.makeText(getActivity(),"ServiceRepaire Exception",Toast.LENGTH_SHORT).show();
                    }
                    catch (GooglePlayServicesNotAvailableException  e)
                    {
                        Toast.makeText(getActivity(),"SeerviceNotAvailable Exception",Toast.LENGTH_SHORT).show();
                    }
                }
            });

            return view;
        }
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == PLACE_PICKER_REQUEST) {
            if (resultCode == RESULT_OK) {
                Place place = PlacePicker.getPlace(data, getActivity());
                String toastMsg = String.format("Place: %s", place.getName());


                Toast.makeText(getActivity(), toastMsg, Toast.LENGTH_LONG).show();
                location.setText(place.getName());
            }
        }
    }
}

这是我实现位置选择器并在onActivityResult上获取位置名称的方式.我可以使用反向地址解析或其他方式获取此信息吗?谁能帮我这个忙吗?谢谢.

This is how I implemented place picker and getting the place name onActivityResult. Can I get this using reverse geocode or something? Can anyone help me out with this please? Thank you.

推荐答案

解决问题的最佳方法是使用Geocoder从经度和纬度获取地址,但是如果出于任何原因,如果您不想使用它,请按照以下说明操作解决方法可能会对您有所帮助.

Best way to solve your problem is using Geocoder to get address from latitude and longitude but if by any reason if you don't want to use it following workaround may help you.

尽管它不是100%可靠,但您可以使用它,直到Google提供这些详细信息为止.在某些随机情况下,它可能无法给出理想的结果,但对大多数情况而言,是可行的.

Though it is not 100% reliable you can use it till Google provides those detailed information. It may not give perfect result for some random cases but works for most.

 public void getAddressDetails(Place place) {
        if (place.getAddress() != null) {
            String[] addressSlice = place.getAddress().toString().split(", ");
            country = addressSlice[addressSlice.length - 1];
            if (addressSlice.length > 1) {
                String[] stateAndPostalCode = addressSlice[addressSlice.length - 2].split(" ");
                if (stateAndPostalCode.length > 1) {
                    postalCode = stateAndPostalCode[stateAndPostalCode.length - 1];
                    state = "";
                    for (int i = 0; i < stateAndPostalCode.length - 1; i++) {
                        state += (i == 0 ? "" : " ") + stateAndPostalCode[i];
                    }
                } else {
                    state = stateAndPostalCode[stateAndPostalCode.length - 1];
                }
            }
            if (addressSlice.length > 2)
                city = addressSlice[addressSlice.length - 3];
            if (addressSlice.length == 4)
                stAddress1 = addressSlice[0];
            else if (addressSlice.length > 3) {
                stAddress2 = addressSlice[addressSlice.length - 4];
                stAddress1 = "";
                for (int i = 0; i < addressSlice.length - 4; i++) {
                    stAddress1 += (i == 0 ? "" : ", ") + addressSlice[i];
                }
            }
        }
        if(place.getLatLng()!=null)
        {
            latitude = "" + place.getLatLng().latitude;
            longitude = "" + place.getLatLng().longitude;
        }
    }

这篇关于如何从地点选择器的地址获取国家,城市?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆