如何在自动填充位置中获取城市和州名_ Places API

时间:2019-05-27 07:04:35

标签: android google-places-api google-places googleplacesautocomplete

我使用“地方信息自动填充”功能从用户那里选择城市,其运行正常, 但是现在我想要城市和州名。 我的代码。

初始化

  List<Place.Field> fields = Arrays.asList(Place.Field.ID, Place.Field.NAME);
            Intent intent = new Autocomplete.IntentBuilder(
                    AutocompleteActivityMode.OVERLAY, fields)
                    .setTypeFilter(TypeFilter.CITIES)
                    .setCountry("IN")
                    .build(this);
            startActivityForResult(intent, AUTOCOMPLETE_REQUEST_CODE);

onActivity结果

if (requestCode == AUTOCOMPLETE_REQUEST_CODE) {
            if (resultCode == RESULT_OK) {
                //Place place = Autocomplete.getPlaceFromIntent(data);
                Place place = Autocomplete.getPlaceFromIntent(data);

                edit_profile_city_editText.setText(place.getName());

            } else if (resultCode == AutocompleteActivity.RESULT_ERROR) {
                Status status = Autocomplete.getStatusFromIntent(data);
                Log.i("Autocomplete error", status.getStatusMessage());
            } else if (resultCode == RESULT_CANCELED) {
            }
        }

从搜索栏中选择城市时,它同时显示城市和州。 例如:钦奈 印度泰米尔纳德邦 请帮助还获取州名...

3 个答案:

答案 0 :(得分:2)

尝试类似的操作:

 Geocoder geocoder = new Geocoder(getContext(), Locale.getDefault());
 List<Address> addresses = new ArrayList<>();
 addresses = geocoder.getFromLocation(lat, lng, 1);
 String country = addresses.get(0).getCountryName();

答案 1 :(得分:1)

我认为这些对您有用;

      Gate::define('create-role', function ( $permission) {
      $id = Auth::user()->id;
      $permissionid = 1;

      //your checking goes here
      });

Geocoder geocoder = new Geocoder(this, Locale.getDefault());
List<Address> addresses = geocoder.getFromLocation(MyLat, MyLong, 1);
String cityName = addresses.get(0).getAddressLine(0);
String stateName = addresses.get(0).getAddressLine(1);
String countryName = addresses.get(0).getAddressLine(2);

答案 2 :(得分:1)

像这样使用:-

和onActivityResult

 if (requestCode == AUTOCOMPLETE_REQUEST_CODE) {
        if (resultCode == RESULT_OK) {
            Place place = Autocomplete.getPlaceFromIntent(data);
            LatLng latLng = place.getLatLng();
            double MyLat = latLng.latitude;
            double MyLong = latLng.longitude;
            Geocoder geocoder = new Geocoder(EditProfileActivity.this, Locale.getDefault());
            try {
                List<Address> addresses = geocoder.getFromLocation(MyLat, MyLong, 1);
                String stateName = addresses.get(0).getAdminArea();
                String cityName = addresses.get(0).getLocality();
                edit_profile_city_editText.setText(place.getName() + "," + stateName);
            } catch (IOException e) {
                e.printStackTrace();

            }

希望这会对您有所帮助。谢谢。