首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >Android Place接口: PlaceDetectionApi.getCurrentPlace()总是不返回结果

Android Place接口: PlaceDetectionApi.getCurrentPlace()总是不返回结果
EN

Stack Overflow用户
提问于 2015-08-28 02:39:50
回答 3查看 2.5K关注 0票数 4

我已经使用Android的Place API来尝试使用Places.PlaceDetectionApi.getCurrentPlace()来获取附近的位置。

然而,当我运行应用程序并尝试获取附近的位置时,我总是得不到任何结果。

我确信我已经设置了权限"ACCESS_FINE_LOCATION",并启用了"Google Place API",还设置了相应的API_KEY。

下面是我使用的活动。

代码语言:javascript
代码运行次数:0
运行
复制
public class PlaceLikelihoodActivity extends Activity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {

private GoogleApiClient mGoogleApiClient;

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

    mGoogleApiClient = new GoogleApiClient
            .Builder(this)
            .addApi(LocationServices.API)
            .addApi(Places.GEO_DATA_API)
            .addApi(Places.PLACE_DETECTION_API)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .build();
}

//function triggered by a button to get nearby places
public void onPlaceLikelihood(View view) {
    Log.i("Place", "onPlaceLikelihood");
    if (mGoogleApiClient != null) {

        PendingResult<PlaceLikelihoodBuffer> result = Places.PlaceDetectionApi
                .getCurrentPlace(mGoogleApiClient, null);

        result.setResultCallback(new ResultCallback<PlaceLikelihoodBuffer>() {
            @Override
            public void onResult(PlaceLikelihoodBuffer likelyPlaces) {
                Log.i("Place", "onResult");
                if (likelyPlaces.getCount() <= 0) {
                    Toast.makeText(PlaceLikelihoodActivity.this, "No place found", Toast.LENGTH_SHORT).show();
                }
                for (PlaceLikelihood placeLikelihood : likelyPlaces) {
                    Log.i("Place", String.format("Place '%s' has likelihood: %g",
                            placeLikelihood.getPlace().getName(),
                            placeLikelihood.getLikelihood()));
                }
                likelyPlaces.release();
            }
        });
    }
    else{
        Toast.makeText(PlaceLikelihoodActivity.this, "No GoogleApiClient", Toast.LENGTH_SHORT).show();
    }

}


@Override
protected void onStart() {
    super.onStart();
    mGoogleApiClient.connect();
}

@Override
protected void onStop() {
    mGoogleApiClient.disconnect();
    super.onStop();
}

@Override
public void onConnected(Bundle bundle) {
    Toast.makeText(this, "onConnected", Toast.LENGTH_LONG).show();

}

@Override
public void onConnectionSuspended(int i) {
    Toast.makeText(this, "onConnectionSuspended", Toast.LENGTH_LONG).show();
}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
    Toast.makeText(this, "onConnectionFailed", Toast.LENGTH_LONG).show();
}
}
EN

回答 3

Stack Overflow用户

发布于 2016-02-13 06:04:40

请尝试在onCreate方法中调用mGoogleApiClient.connect();。除了onPlaceLikelihood中的mGoogleApiClient != null之外,还可以检查mGoogleApiClient.isConnected()。

票数 0
EN

Stack Overflow用户

发布于 2016-06-24 22:53:15

您是否在Google开发者控制台中启用了API "Google Places API for Android“?再见

票数 0
EN

Stack Overflow用户

发布于 2017-03-13 17:14:00

我想我知道答案,在模拟器或你的手机上,转到隐私和安全,并打开位置高精度模式。

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32257103

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档