如何在谷歌地方选择器上获得城市

时间:2018-01-29 20:37:27

标签: android google-places-api

  try {
                addresses = gcd.getFromLocation(place.getLatLng().latitude, place.getLatLng().longitude, 1);
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (addresses.size() > 0) {
                String toastMsg = String.format("Place: %s", addresses.get(0).getLocale() + " - " + addresses.get(0).getCountryName() + " - " + addresses.get(0).getCountryCode());
                Toast.makeText(this, toastMsg, Toast.LENGTH_LONG).show();


            }

1 个答案:

答案 0 :(得分:0)

我找到了解决方案,谢谢所有开发者android

  Geocoder gcd = new Geocoder(getActivity(), Locale.getDefault());
        List<Address> addresses = null;
        try {

            addresses = gcd.getFromLocation(place.getLatLng().latitude, place.getLatLng().longitude, 1);
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (addresses.size() > 0) {

            /*
            getCity --> addresses.get(0).getLocality()
            getCountry --> addresses.get(0).getCountryName()
            getCodeCountry --> addresses.get(0).getCountryCode() (MA)
             */

            city = String.format("%s",addresses.get(0).getLocality());
            country= String.format("%s",addresses.get(0).getCountryName());

            tvCountry.setText(country);
            tvCity.setText(city);

        }