- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
在适用于 Android 的 Google Places API 中,我使用 PlaceAutocompleteFragment 来显示城市/国家/地区。
这里是获取地址、姓名、placeId等Place
对象只包含这些字段。
@Override
public void onPlaceSelected(Place place) {
Log.i(TAG, "Place Selected: " + place.getName());
// Format the returned place's details and display them in the TextView.
mPlaceDetailsText.setText(formatPlaceDetails(getResources(), place.getName(), place.getId(),
place.getAddress(), place.getPhoneNumber()+" "+place.getAttributions()+" :: "+place.getLocale(), place.getWebsiteUri()));
}
但我也想要国家代码。有没有办法从 places API 获取国家代码?如果没有,是否有任何替代服务可以在键入时获取国家/地区名称和代码?
最佳答案
通过检索 Place 对象,您可以获得关联的 Locale 对象:
Locale locale = place.getLocale();
使用此 Locale 对象,您可以通过以下方式获取国家/地区代码:
locale.getCountry();
国家名称:
locale.getDisplayCountry();
您可以在文档中查看更多可用方法: http://developer.android.com/reference/java/util/Locale.html
编辑:
如果 Place 对象的 Locale 为空,您可以使用 Geocoder 从 GPS 坐标获取信息:
LatLng coordinates = place.getLatLng(); // Get the coordinates from your place
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
List<Address> addresses = geocoder.getFromLocation(
coordinates.latitude,
coordinates.longitude,
1); // Only retrieve 1 address
Address address = addresses.get(0);
然后你可以调用这些方法来获取你想要的信息
address.getCountryCode();
address.getCountryName();
地址对象的更多方法:http://developer.android.com/reference/android/location/Address.html
请注意,应在后台线程上调用 Geocoder 方法以不阻塞 UI,并且它也可能检索不到任何答案。
关于android - 从 PlaceAutocompleteFragment android (Google Places API) 获取国家代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37051900/
我想实现 AutocompleteTextView(google places),但是当我点击 fragment 中的 searchView 时, fragment 消失了(掉下来)。 我的代码:
有没有什么方法可以使用 Google Places API 中的 placeautocompletefragment 获取在搜索 View 中显示的完整地址?它只显示整个地址的第一部分。我确信一切都已
在我的应用程序中,我添加了 Google map ,它们运行良好。我想添加一个 PlaceAutocompleteFragment。我正在使用我已经为 map 生成的 key ,并且我已经从 goog
如何解决PlaceAutocompleteFragment? 我正在尝试使用新的 PlaceAutocompleteFragment。我尝试使用以下代码: 此代码示例只是一个副本。来自这里site
这是我的 .xml 文件 我添加了 PlaceAutocompleteFragment,但它显示未解析的类 PlaceAutocompleteFragment。我检查了依赖文件,所有的东西都被添加了。
在我的应用程序主页中,google place auto complete fragment 在对话框栏内按下按钮打开,该位置用于搜索查询。问题是当我尝试通过再次按下按钮来更改位置时,应用程序崩溃并出
我在 setHint 方法上收到 NPE。Logcat 显示此错误: void android.widget.EditText.setHint(java.lang.CharSequence)' on
我想在 Activity 加载完成后立即自动点击 fragment 。 fragment 定义为: 我试过这样做 fragment = findViewById(R.id.place_autoco
我在最近的一个项目中使用了 Google 的 PlaceAutoCompleteFragment,目前我正在从事这项工作。当我快速单击 PlaceAutoCompleteFragment 时,它会在我
我想在我的 Android 应用程序中实现 placeautocompletefragment。我正在尝试使用 this url 中的说明但它显示: can not resolve symbol fo
我正在使用 PlaceAutocompleteFragment 来搜索地点。下面是我的xml代码 我想删除默认带有 fragment 的搜索图标。 最佳答案 您可以使用这一行来获取搜索图标的引用:(
我正在使用 Google Places API for Android - PlaceAutocompleteFragment 我项目中用于搜索位置的 API,当用户选择一个位置然后使用位置经纬度获取
我正在使用 Google Place获取地点详情。 Google 提供了不同的方式来实现 Google Place API 以获取地点详细信息。不同的方式如 PlaceAutocompleteFrag
我在 Fragment 中使用 PlaceAutoCompleteFragment。第一次打开 Fragment(放置 PlaceAutoCompleteFragment 的 App fragment
我想实现 PlaceAutocompleteFragment based on the docs,在我的应用程序中,但是当我添加此代码段时 最后一行抛出“无法解析符号‘PlaceAutocomple
我已经在 Activity 中实现了 PlaceAutocompleteFragment 并且工作成功。但是如何在android的 fragment 中实现相同的功能?我已经实现了这样的 placea
我正在尝试将 PlaceAutocompleteFragment 添加到我的 Activity 中,这是我正在使用的所有导入: import com.google.android.gms.common
我在 fragment 中使用 PlaceAutoCompleteFragment。第一次打开 Fragment(其中放置了 PlaceAutoCompleteFragment 的 App fragm
我正在按照官方文档 here 尝试将一个 Place Autocomplete fragment 添加到我的 fragment 中 我收到错误 kotlin.TypeCastException: nu
我正在尝试使用 Google 的地点自动完成 ( https://developers.google.com/places/android-api/autocomplete ) 在我的应用程序中的 f
我是一名优秀的程序员,十分优秀!