【问题标题】:Country Specific google place autocomplete国家特定的谷歌地方自动完成
【发布时间】:2019-05-25 11:01:49
【问题描述】:

我正在关注http://examples.javacodegeeks.com/android/android-google-places-autocomplete-api-example/ 这个教程,以便在我的应用中获取 google place 自动完成功能, 现在它工作正常,但我需要保持特定国家/地区的位置建议,我曾尝试提供像印度、中国这样的国家,但没有显示任何结果。 能否请您帮帮我,告诉我应该在哪里更改代码才能完成。

谢谢, 普拉尚特

【问题讨论】:

  • 该教程中的方法是旧的json解析。如果您使用 android 的自动完成功能。可以参考here。您也可以从here 获得演示。我试过了,效果很好。对于特定国家/地区,您可以使用 LatLngBounds 对象,将结果限制在由纬度和经度边界指定的特定区域。

标签: android google-maps autocomplete


【解决方案1】:

来自 Places API 文档

要获取预测的地名和/或地址列表,请调用 GeoDataApi.getAutocompletePredictions(),传递以下内容 参数:

必需:包含用户键入的文本的查询字符串。

必需:LatLngBounds 对象,将结果限制为特定的 由纬度和经度界限指定的区域。

请求时

您可以指定印度的边界。

【讨论】:

  • 谢谢,但我想让它像用户所在的任何地方一样通用,他的国家绑定将被动态设置。
  • @hiccup 你能根据国家动态找到边界吗?
【解决方案2】:

这可以为您服务

    String apiKey = getString(R.string.places_api_key);
        if (!Places.isInitialized()) {
            Places.initialize(getApplicationContext(), apiKey);
            PlacesClient placesClient = Places.createClient(this);
        }else{
            Toast.makeText(PlaceAutocompleteActivity.this, "-----initialize-----", Toast.LENGTH_LONG).show();
        }

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

        // PlaceFields
        autocompleteFragment.setPlaceFields(Arrays.asList(Place.Field.ID, Place.Field.NAME, Place.Field.LAT_LNG));

        // autocompleteFragment.setOnPlaceSelectedListener(MapsActivity.this);

        try {

            autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
                @Override
                public void onPlaceSelected(@NonNull Place place) {
                    displayLocation(place.getLatLng().latitude,place.getLatLng().longitude,place.getName());
                    Toast.makeText(getApplicationContext(), "getName: " + place.getName() + " getLatLng: "+ place.getLatLng(), Toast.LENGTH_LONG).show();

//                    Intent i = Intent(this, idnow.se MainActivity.class);
//                    i.putExtra("getLatLng",place.getLatLng());
//                    startActivity(i);
//                    finish();
                }

                @Override
                public void onError(@NonNull Status status) {
                    Toast.makeText(getApplicationContext(), "" + status.toString(), Toast.LENGTH_LONG).show();

                }

            });
        } catch (Exception e) {
            Toast.makeText(PlaceAutocompleteActivity.this, e.toString(), Toast.LENGTH_LONG).show();
        }

// PlaceFields - 这里

autocompleteFragment.setPlaceFields(Arrays.asList(Place.Field.ID, Place.Field.NAME, Place.Field.LAT_LNG));

https://github.com/EddyEU/GooglePlaceAutocomplete

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-25
    • 2012-06-12
    • 2015-10-23
    • 1970-01-01
    • 1970-01-01
    • 2017-12-23
    相关资源
    最近更新 更多