如何从Google PlaceAutoComplete片段获取完整地址?

时间:2017-03-09 12:29:55

标签: android location google-places

我希望在选择位置时获取完整地址。以下是我的方案示例。

enter image description here

当我选择Uttara时,如果我应用place.getName(),我只得到Uttara。如果我应用place.getAddress(),我只会得到孟加拉国的Uttara。我如何才能获得孟加拉国达卡达卡的Uttara?请帮助伙计.....

这是我的代码段:

 PlaceAutocompleteFragment places= (PlaceAutocompleteFragment)
                getFragmentManager().findFragmentById(R.id.place_autocomplete_fragment_pick);
        LinearLayout ll = (LinearLayout) places.getView().findViewById(R.id.place_autocomplete_fragment_pick);
        ll.setPadding(-7,-7,-7,-7);
        etPickPlace = (EditText)places.getView().findViewById(R.id.place_autocomplete_search_input);
        etPickPlace.setTextColor(getResources().getColor(R.color.colorPrimaryDark));
        etPickPlace.setTextSize(18.0f);
        AutocompleteFilter typeFilter = new AutocompleteFilter.Builder()
                .setCountry("BD")
                .build();

        places.setFilter(typeFilter);
        places.setOnPlaceSelectedListener(new PlaceSelectionListener() {
            @Override
            public void onPlaceSelected(Place place) {
                //Here I want to get the full pick up address
                pickAddress = place.getAddress().toString();
              //  Toast.makeText(getApplicationContext(),place.getName(),Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onError(Status status) {
                Toast.makeText(getApplicationContext(),status.toString(), Toast.LENGTH_SHORT).show();

            }

        });

1 个答案:

答案 0 :(得分:4)

你可以place.getAddess()这样做。如果您想要完整地址并且更加正确,则需要实现GeoCode API。

下课是反向地理编码的关键元素,用于获取传递的纬度和经度坐标的地址。我们在此处访问输入代码的Geocoder Google API以进行反向地理编码,并获取街道,城市,地址/邮政编码等各个地址。

LocationAddress.java

public class LocationAddress {

private static final String TAG = "LocationAddress";
public static void getAddressFromLocation(final double latitude, final double longitude,
                                          final Context context, final Handler handler) {
    Thread thread = new Thread() {
        @Override
        public void run() {
            Geocoder geocoder = new Geocoder(context, Locale.getDefault());
            String result = null;
            try {
                List<Address> addressList = geocoder.getFromLocation(
                        latitude, longitude, 1);
                if (addressList != null && addressList.size() > 0) {
                    Address address = addressList.get(0);
                    StringBuilder sb = new StringBuilder();
                    for (int i = 0; i < address.getMaxAddressLineIndex(); i++) {
                        sb.append(address.getAddressLine(i)).append("\n");
                    }
                    sb.append(address.getLocality()).append("\n");
                    sb.append(address.getPostalCode()).append("\n");
                    sb.append(address.getCountryName());
                    result = sb.toString();
                }
            } catch (IOException e) {
                Log.e(TAG, "Unable connect to Geocoder", e);
            } finally {
                Message message = Message.obtain();
                message.setTarget(handler);
                if (result != null) {
                    message.what = 1;
                    Bundle bundle = new Bundle();
                    result = "Latitude: " + latitude + " Longitude: " + longitude +
                            "\n\nAddress:\n" + result;
                    bundle.putString("address", result);
                    message.setData(bundle);
                } else {
                    message.what = 1;
                    Bundle bundle = new Bundle();
                    result = "Latitude: " + latitude + " Longitude: " + longitude +
                            "\n Unable to get address for this lat-long.";
                    bundle.putString("address", result);
                    message.setData(bundle);
                }
                message.sendToTarget();
            }
        }
    };
    thread.start();
}
}

希望这有助于满足您的要求。

供参考:Click Here

快乐编码!!!