如何从Android Place选择器获取城市和乡村?

时间:2017-09-22 10:11:17

标签: android google-places-api

请帮忙。我尝试实施Place picker以获取城市名称和国家/地区。但它仍然没有结果。

在NewItemFragment中

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

            //TODO QIBEE Insert
            PlacePicker.IntentBuilder intentBuilder = new PlacePicker.IntentBuilder();
            try {
                Intent intent = intentBuilder.build(getActivity());
                getActivity().startActivityForResult(intent,REQ_CODE);
            } catch (GooglePlayServicesRepairableException | GooglePlayServicesNotAvailableException e) {
                e.printStackTrace();
            }
App.java中的

  Geocoder geocoder;
            List<Address> addresses;
            geocoder = new Geocoder(getApplicationContext(), Locale.ENGLISH);

            try {

                addresses = geocoder.getFromLocation(latitude, longitude, 1);

                if (addresses.size() > 0) {

                    App.getInstance().setLat(latitude);
                    App.getInstance().setLng(longitude);

                    sharedPref.edit().putString(getString(R.string.settings_account_lat), Double.toString(this.getLat())).apply();
                    sharedPref.edit().putString(getString(R.string.settings_account_lng), Double.toString(this.getLng())).apply();

                    App.getInstance().setArea(addresses.get(0).getAdminArea());
                    App.getInstance().setCity(addresses.get(0).getLocality());
                    App.getInstance().setCountry(addresses.get(0).getCountryName());

2 个答案:

答案 0 :(得分:2)

你将从onActivityResult获得longditute和latitiude并使用参数latitiude和longditute调用这个函数getAddress

public void getAddress(double lat, double lng) {
    Geocoder geocoder = new Geocoder(this, Locale.getDefault());
    try {
        List<Address> addresses = geocoder.getFromLocation(lat, lng, 1);
        Address obj = addresses.get(0);
        String add = obj.getAddressLine(0);
        add = add + "\n" + obj.getCountryName();
        add = add + "\n" + obj.getCountryCode();
        add = add + "\n" + obj.getAdminArea();
        add = add + "\n" + obj.getPostalCode();
        add = add + "\n" + obj.getSubAdminArea();
        add = add + "\n" + obj.getLocality();
        add = add + "\n" + obj.getSubThoroughfare();
        Log.v("IGA", "Address" + add);
        // Toast.makeText(this, "Address=>" + add,
        // Toast.LENGTH_SHORT).show();

        // TennisAppActivity.showDialog(add);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

答案 1 :(得分:0)

PlaceAutocompleteFragmentactivity,将一个名为com.google.android.gms.location.places.ui.PlaceAutocompleteFragment的新片段添加到XML布局中。

<fragment
  android:id="@+id/place_autocomplete_fragment"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment"
  />

PlaceSelectionListener添加到activity

PlaceSelectionListener处理返回地点以响应用户的选择。以下代码显示了创建对fragment的引用并向listener添加PlaceAutocompleteFragment

PlaceAutocompleteFragment autocompleteFragment = (PlaceAutocompleteFragment)
getFragmentManager().findFragmentById(R.id.place_autocomplete_fragment);

autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
    @Override
    public void onPlaceSelected(Place place) {
        // TODO: Get info about the selected place.
        Log.i(TAG, "Place: " + place.getName());
    }

    @Override
    public void onError(Status status) {
        // TODO: Handle the error.
        Log.i(TAG, "An error occurred: " + status);
    }
  });