英文:
How to restrict to particular city results using Places API android
问题
我有一个需求,如果假设城市是海得拉巴,那么我需要使用地点搜索API仅在搜索时获取海得拉巴的地点。但是我在搜索时只得到了一些地点。如何获取与海得拉巴城市相关的所有地点。请问是否有人可以帮助我。
> **我已尝试的代码:**
```java
if (!Places.isInitialized()) {
Places.initialize(getApplicationContext(), "apikey");
}
final AutocompleteSupportFragment autocompleteFragment = (AutocompleteSupportFragment)
getSupportFragmentManager().findFragmentById(R.id.autofill);
autocompleteFragment.setCountry("IN");
final Button closeBtn = dialog.findViewById(R.id.close_btn);
dialog.show();
autocompleteFragment.setLocationRestriction(RectangularBounds.newInstance(
new LatLng(17.387140, 78.491684),
new LatLng(17.440081, 78.348915)));
autocompleteFragment.setPlaceFields(Arrays.asList(Place.Field.ID, Place.Field.NAME, Place.Field.ADDRESS, Place.Field.LAT_LNG));
autocompleteFragment.setTypeFilter(TypeFilter.ADDRESS);
autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
@Override
public void onPlaceSelected(Place place) {
// TODO: 获取所选地点的信息。
if (place.getAddress() != null) {
Log.e("Place", "Place: " + place.getName() + " , " + place.getId() + " , " + place.getAddress() + " , " + place.getLatLng());
double latitude = Objects.requireNonNull(place.getLatLng()).latitude;
Log.d("lat:", String.valueOf(latitude));
double longitude = place.getLatLng().longitude;
Log.d("lng:", String.valueOf(longitude));
String name = place.getName();
Geocoder geocoder = new Geocoder(PlacesSearch.this, Locale.getDefault());
List<Address> addresses = null;
try {
addresses = geocoder.getFromLocation(latitude, longitude, 1);
} catch (IOException e) {
e.printStackTrace();
}
String aa = addresses.get(0).getAddressLine(0);
Log.e("aa:", aa);
String[] ad = aa.split(",");
String street = ad[0] + "," + ad[1] + "," + ad[2];
String city1 = addresses.get(0).getLocality();
String state1 = addresses.get(0).getAdminArea();
String zip1 = addresses.get(0).getPostalCode();
String country1 = addresses.get(0).getCountryName();
String countryCode = addresses.get(0).getCountryCode();
System.out.println("countryCode" + countryCode);
placesearch.setText(aa);
dialog.dismiss();
}
}
@Override
public void onError(Status status) {
// TODO: 处理错误。
Log.e("Place", "An error occurred: " + status);
}
});
英文:
I had a requirement that if suppose the city is Hyderabad then using places search API I need to get only Hyderabad places while searching. But I am getting only a few places in search. how to get all the places related to Hyderabad city. Please anyone can help me out.
> I have tried the code:
if (!Places.isInitialized()) {
Places.initialize(getApplicationContext(), "apikey");
}
final AutocompleteSupportFragment autocompleteFragment = (AutocompleteSupportFragment)
getSupportFragmentManager().findFragmentById(R.id.autofill);
autocompleteFragment.setCountry("IN");
final Button closeBtn = dialog.findViewById(R.id.close_btn);
dialog.show();
autocompleteFragment.setLocationRestriction(RectangularBounds.newInstance(
new LatLng(17.387140, 78.491684),
new LatLng(17.440081, 78.348915)));
autocompleteFragment.setPlaceFields(Arrays.asList(Place.Field.ID, Place.Field.NAME, Place.Field.ADDRESS, Place.Field.LAT_LNG));
autocompleteFragment.setTypeFilter(TypeFilter.ADDRESS);
autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
@Override
public void onPlaceSelected(Place place) {
// TODO: Get info about the selected place.
if (place.getAddress() != null) {
Log.e("Place", "Place: " + place.getName() + ", " + place.getId() + " , " + place.getAddress() + " , " + place.getLatLng());
// progressDialog.show();
// progressDialog.setCancelable(false);
double latitude = Objects.requireNonNull(place.getLatLng()).latitude;
Log.d("lat:", String.valueOf(latitude));
double longitude = place.getLatLng().longitude;
Log.d("lng:", String.valueOf(longitude));
String name = place.getName();
Geocoder geocoder = new Geocoder(PlacesSearch.this, Locale.getDefault());
List<Address> addresses = null;
try {
addresses = geocoder.getFromLocation(latitude, longitude, 1);
} catch (IOException e) {
e.printStackTrace();
}
String aa = addresses.get(0).getAddressLine(0);
Log.e("aa:", aa);
String[] ad = aa.split(",");
String street = ad[0] + "," + ad[1] + "," + ad[2];
String city1 = addresses.get(0).getLocality();
String state1 = addresses.get(0).getAdminArea();
String zip1 = addresses.get(0).getPostalCode();
String country1 = addresses.get(0).getCountryName();
String countryCode = addresses.get(0).getCountryCode();
System.out.println("countryCode"+countryCode);
placesearch.setText(aa);
// progressDialog.dismiss();
dialog.dismiss();
}
}
@Override
public void onError(Status status) {
// TODO: Handle the error.
Log.e("Place", "An error occurred: " + status);
}
});
专注分享java语言的经验与见解,让所有开发者获益!
评论