如何使用Places API for Android限制特定城市的结果。

huangapple 未分类评论55阅读模式
英文:

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(), &quot;apikey&quot;);
                }

                final AutocompleteSupportFragment autocompleteFragment = (AutocompleteSupportFragment)
                        getSupportFragmentManager().findFragmentById(R.id.autofill);

                autocompleteFragment.setCountry(&quot;IN&quot;);
                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(&quot;Place&quot;, &quot;Place: &quot; + place.getName() + &quot;, &quot; + place.getId() + &quot; , &quot; + place.getAddress() + &quot; , &quot; + place.getLatLng());

//                            progressDialog.show();
//                            progressDialog.setCancelable(false);

                          double  latitude = Objects.requireNonNull(place.getLatLng()).latitude;
                            Log.d(&quot;lat:&quot;, String.valueOf(latitude));
                          double  longitude = place.getLatLng().longitude;
                            Log.d(&quot;lng:&quot;, String.valueOf(longitude));

                            String name = place.getName();

                            Geocoder geocoder = new Geocoder(PlacesSearch.this, Locale.getDefault());

                            List&lt;Address&gt; addresses = null;
                            try {
                                addresses = geocoder.getFromLocation(latitude, longitude, 1);

                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                            String aa = addresses.get(0).getAddressLine(0);
                            Log.e(&quot;aa:&quot;, aa);
                            String[] ad = aa.split(&quot;,&quot;);

                            String street = ad[0] + &quot;,&quot; + ad[1] + &quot;,&quot; + 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(&quot;countryCode&quot;+countryCode);

                          placesearch.setText(aa);
                            //    progressDialog.dismiss();

                            dialog.dismiss();


                        }

                    }

                    @Override
                    public void onError(Status status) {
                        // TODO: Handle the error.
                        Log.e(&quot;Place&quot;, &quot;An error occurred: &quot; + status);
                    }
                });

huangapple
  • 本文由 发表于 2020年7月27日 20:01:51
  • 转载请务必保留本文链接:https://java.coder-hub.com/63114908.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定