Google Places API未根据位置返回建议 [英] Google Places API not returning suggestions based on location

查看:107
本文介绍了Google Places API未根据位置返回建议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我从Google地方信息自动填充功能中搜索时,它会提供来自世界各地的建议/预测.但是我想根据用户的位置获得建议.我试图在模拟器中手动更改位置,但是仍然无法正常工作.我认为我的代码中可能缺少某些内容,但是我不知道是什么.请帮忙.

When I search from the Google Places Autocomplete, it gives suggestions/predictions from all around the world. However I want to get suggestions based on user's location. I have tried to change my location manually in the emulator, but it still does not work. I think I may be lacking something in my code, but I don't know what. Please help.

代码:

我的依赖关系

compile 'com.google.android.gms:play-services-places:10.2.1'
compile 'com.google.android.gms:play-services-location:10.2.1'
compile 'com.google.android.gms:play-services:10.2.1'

查找位置活动

public class FindLocation extends AppCompatActivity 
        implements GoogleApiClient.ConnectionCallbacks, 
        GoogleApiClient.OnConnectionFailedListener {

    private int PLACE_AUTOCOMPLETE_REQUEST_CODE = 1;
    private GoogleApiClient mGoogleApiClient;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_find_location);

        mGoogleApiClient = new GoogleApiClient
                .Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(Places.GEO_DATA_API)
                .addApi(Places.PLACE_DETECTION_API)
                .enableAutoManage(this, this)
                .build();

        try {
            Intent intent =
                    new PlaceAutocomplete.IntentBuilder(PlaceAutocomplete.MODE_OVERLAY)
                            .build(this);
            startActivityForResult(intent, PLACE_AUTOCOMPLETE_REQUEST_CODE);
        } catch (GooglePlayServicesRepairableException e) {
            // TODO: Handle the error.
        } catch (GooglePlayServicesNotAvailableException e) {
            // TODO: Handle the error.
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == PLACE_AUTOCOMPLETE_REQUEST_CODE) {
            if (resultCode == RESULT_OK) {
                Place place = PlaceAutocomplete.getPlace(this, data);
                //Log.i(TAG, "Place: " + place.getName());
            } else if (resultCode == PlaceAutocomplete.RESULT_ERROR) {
                Status status = PlaceAutocomplete.getStatus(this, data);
                // TODO: Handle the error.
                //Log.i(TAG, status.getStatusMessage());

            } else if (resultCode == RESULT_CANCELED) {
                // The user canceled the operation.
            }
        }
    }

    @Override
    public void onConnected(@Nullable Bundle bundle) {

        //refreshPlacesData();

    }

    @Override
    public void onConnectionSuspended(int i) {

    }

    @Override
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

    }
}

我的活动的XML

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.android.project_water.FindLocation">

</android.support.constraint.ConstraintLayout>

推荐答案

我找到了解决方案,并感谢Mahesh Gawhane和Patrick R帮助我.但是为了完全回答我的问题,我必须首先根据纬度和经度来获取用户的当前位置.我遵循了教程根据纬度和经度来获取用户的位置(取决于您的应用程序是什么,您可以根据需要自定义代码).

I have found the solution, and thanks to Mahesh Gawhane and Patrick R for helping me. But in order to answer my question fully, I had to first get the user's current location in terms of Latitude and Longitude. I followed this tutorial to get the user's location in terms of Lat and Lng (depending on what your application is, you can customise the code to your need).

然后,我必须将此Lat和Lng坐标输入到LatLngBounds对象中,然后按照教程进行操作这样做.有了LatLngBounds对象后,我将setBoundsBias()方法调用到Places Autocomplete中,请参见指南这里.

Then, I had to input this Lat and Lng coordinates into a LatLngBounds object, of which, I followed this tutorial to do so. Once I had the LatLngBounds object, I called the setBoundsBias() method into the Places Autocomplete, see guide here.

现在,地方信息自动填充"功能将根据用户位置周围的某个有界"区域给出偏见"建议! 偏见"是指相对于不在此有界"区域内的地方.

Now, the Places Autocomplete will give 'bias' suggestions based on a certain 'bounded' area around the user's location! By 'bias' I mean relative to places not within this 'bounded' area.

这篇关于Google Places API未根据位置返回建议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆