gpt4 book ai didi

android - 如何从地点选择器的地址获取国家,城市?

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:01:52 26 4
gpt4 key购买 nike

我正在使用地点选择器的 Intent 来获取地点。现在我想将地址以国家、城市、密码、州的形式保存。我怎样才能从地点选择器的地址得到所有这些?

代码:

public class NameOfBusinessFragment extends Fragment {

int PLACE_PICKER_REQUEST = 1;
int RESULT_OK = -1;
PlacePicker.IntentBuilder builder = new PlacePicker.IntentBuilder();
EditText category,location;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_name_of_business,
container, false);

location = (EditText)view.findViewById(R.id.location);

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

try {

startActivityForResult(builder.build(getActivity()), PLACE_PICKER_REQUEST);
}
catch (GooglePlayServicesRepairableException e)
{
Toast.makeText(getActivity(),"ServiceRepaire Exception",Toast.LENGTH_SHORT).show();
}
catch (GooglePlayServicesNotAvailableException e)
{
Toast.makeText(getActivity(),"SeerviceNotAvailable Exception",Toast.LENGTH_SHORT).show();
}
}
});

return view;
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PLACE_PICKER_REQUEST) {
if (resultCode == RESULT_OK) {
Place place = PlacePicker.getPlace(data, getActivity());
String toastMsg = String.format("Place: %s", place.getName());


Toast.makeText(getActivity(), toastMsg, Toast.LENGTH_LONG).show();
location.setText(place.getName());
}
}
}
}

这就是我实现地点选择器并在 ActivityResult 上获取地点名称的方式。我可以使用反向地理编码或其他方式获得它吗?谁能帮我解决这个问题?谢谢。

最佳答案

我认为直接使用 Place 类是不可能的,但你可以要求它从纬度和经度开始谷歌。下面是您的 onActivityResult 方法的示例:

public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PLACE_PICKER_REQUEST) {
if (resultCode == RESULT_OK) {
// get place data
Place place = PlacePicker.getPlace(data, getActivity());

// ask for geolocation data
Geocoder gcd = new Geocoder(this, 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) {
String toastMsg = String.format("Place: %s", addresses.get(0).getLocality() + " - " + addresses.get(0).getCountryName() + " - " + addresses.get(0).getCountryCode());
Toast.makeText(getActivity(), toastMsg, Toast.LENGTH_LONG).show();


// NOW SET HERE CORRECT DATA
//location.setText(place.getName());

}
}
}
}

关于android - 如何从地点选择器的地址获取国家,城市?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37873415/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com