gpt4 book ai didi

java - 我如何将直接从 Google Place API 获取的地址组件明智地 fork ?

转载 作者:行者123 更新时间:2023-11-29 00:52:21 26 4
gpt4 key购买 nike

我想获取城市、州、国家/地区,并将其固定到不同的变量中,就像获取地址、纬度和经度一样。但我不知道如何按组件方式获取地址。

public class MainActivity extends AppCompatActivity {
String TAG = "placeautocomplete";
String API = "xxxxxxxxxxxxxxxxx";
String Latitude;
String Longitude;
double lat, lng;
String Address,Place_name,Phone,Complete_address;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edit = findViewById(R.id.editText);
txtView = findViewById(R.id.txtView);
// Initialize Places.
Places.initialize(getApplicationContext(), API);
// Create a new Places client instance.
PlacesClient placesClient = Places.createClient(this);

// Initialize the AutocompleteSupportFragment.
if (!Places.isInitialized()) {
Places.initialize(getApplicationContext(), API);


}

// Initialize the AutocompleteSupportFragment.
AutocompleteSupportFragment autocompleteFragment = (AutocompleteSupportFragment)
getSupportFragmentManager().findFragmentById(R.id.autocomplete_fragment);

assert autocompleteFragment != null;

autocompleteFragment.setPlaceFields(Arrays.asList(Place.Field.ID, Place.Field.NAME,Place.Field.LAT_LNG, Place.Field.ADDRESS,Place.Field.PHONE_NUMBER));
autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
@Override
public void onPlaceSelected(Place place) {
// TODO: Get info about the selected place.
Log.i(TAG, "Place: " + place.getName() + ", " + place.getId());

if (place.getLatLng() !=null){
lat =place.getLatLng().latitude;
lng =place.getLatLng().longitude;

}
Latitude = String.valueOf(lat);
Longitude= String.valueOf(lng);
Address= place.getAddress();
Place_name = place.getName();
Phone = place.getPhoneNumber();

}
@Override
public void onError(Status status) {
// TODO: Handle the error.
Log.i(TAG, "An error occurred: " + status);
}
});
}
}

我只想在 Address、City、State、Pin 等组件中解决问题。

最佳答案

你不应该使用 Address= place.getAddress()但是.getAddressComponents()这样做,因为 .getAddress() 返回 String具有人类可读的地点地址和谷歌官方文档中写道:

Do not parse the formatted address programmatically. Instead you should use the individual address components, which the API response includes in addition to the formatted address field.

所以,你应该使用 Place.getAddressComponents() 得到List<AddressComponent>然后得到名字和type of each address component .或使用额外的地点请求 lat/lng 和 Geocoder.getFromLocation() 就像在Display a location address官方示例:

...
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
List<Address> addresses = null;

...
addresses = geocoder.getFromLocation(
Latitude, // <-- your Latitude = String.valueOf(lat);
Longitude, // <- your Longitude= String.valueOf(lng);
location.getLongitude(),
// In this sample, get just a single address.
1);

// Handle case where no address was found.
if (addresses == null || addresses.size() == 0) {
if (errorMessage.isEmpty()) {
errorMessage = getString(R.string.no_address_found);
Log.e(TAG, errorMessage);
}
deliverResultToReceiver(Constants.FAILURE_RESULT, errorMessage);
} else {
Address address = addresses.get(0);
ArrayList<String> addressFragments = new ArrayList<String>();

// Fetch the address lines using getAddressLine,
// join them, and send them to the thread.
for(int i = 0; i <= address.getMaxAddressLineIndex(); i++) {
addressFragments.add(address.getAddressLine(i));
}
}
...

另请查看 Developer Guide .

附言不要对变量使用大写名称(例如,Latitude)——这是类的风格。将其命名为 latitude .

关于java - 我如何将直接从 Google Place API 获取的地址组件明智地 fork ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58130587/

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