这里有地址,名字,placeID等.
Place对象仅包含这些字段.
@OverrIDepublic 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()));
}
但我也想要国家代码.有没有办法从地方API获取国家/地区代码?
如果不是,是否有任何替代服务来获取国家名称&键入时的代码?
Locale locale = place.getLocale();
使用此Locale对象,您可以通过以下方式获取Country代码:
locale.getCountry();
和国家名称:
locale.getdisplayCountry();
您可以在文档中查找更多可用的方法:
http://developer.android.com/reference/java/util/Locale.html
编辑:
如果Place对象中的Locale为null,则可以使用Geocoder从GPS坐标获取信息:
LatLng coordinates = place.getLatLng(); // Get the coordinates from your placeGeocoder geocoder = new Geocoder(this,Locale.getDefault());List<Address> addresses = geocoder.getFromLocation( coordinates.latitude,coordinates.longitude,1); // Only retrIEve 1 addressAddress address = addresses.get(0);
然后,您可以调用这些方法来获取所需的信息
address.getCountryCode();address.getCountryname();
地址对象上的更多方法:http://developer.android.com/reference/android/location/Address.html
请注意,应该在后台线程上调用Geocoder方法,以阻止UI,并且它也可以检索不到答案.
总结以上是内存溢出为你收集整理的从PlaceAutocompleteFragment android(Google Places API)获取国家/地区代码全部内容,希望文章能够帮你解决从PlaceAutocompleteFragment android(Google Places API)获取国家/地区代码所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)