【问题标题】:Google Place Api gives Status{statusCode=ERROR, resolution=null}Google Place Api 给出 Status{statusCode=ERROR, resolution=null}
【发布时间】:2016-11-08 09:10:51
【问题描述】:

以下是我在当前位置获取附近位置的代码。该代码有时有效,有时在不更改任何内容的情况下无效。为什么会这样。我检查了 api 密钥是否有效,状态代码“错误”表示操作失败,https://developers.google.com/android/reference/com/google/android/gms/common/api/CommonStatusCodes.html#constants 没有更多详细信息 谢谢 以下是活动:

public class PlacesAPIActivity extends AppCompatActivity implements
        OnConnectionFailedListener, GoogleApiClient.ConnectionCallbacks {
    private static final String LOG_TAG = "PPPPPPPPPPPPPPPPP";
    private static final int GOOGLE_API_CLIENT_ID =0;
    private GoogleApiClient mGoogleApiClient;
    private static final int PERMISSION_REQUEST_CODE = 100;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_places_api);
        Log.d("resuktfvvvfdfd", "onCreate: ");
        buildGoogleApiClient();}

    private synchronized void buildGoogleApiClient()
    {  Button currentButton = (Button) findViewById(R.id.currentButton);
        mGoogleApiClient = new GoogleApiClient.Builder(PlacesAPIActivity.this)
                .addApi(Places.PLACE_DETECTION_API)
                .addApi(Places.GEO_DATA_API)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .enableAutoManage(this, GOOGLE_API_CLIENT_ID, this)
                .build();
        mGoogleApiClient.connect();
        Log.d("connected", String.valueOf(mGoogleApiClient.isConnected()));
        currentButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mGoogleApiClient.isConnected()) {
                    Log.d("connected1", String.valueOf(mGoogleApiClient.isConnected()));
                    if (ContextCompat.checkSelfPermission(PlacesAPIActivity.this,
                            Manifest.permission.ACCESS_FINE_LOCATION)
                            != PackageManager.PERMISSION_GRANTED) {
                        ActivityCompat.requestPermissions(PlacesAPIActivity.this,
                                new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                                PERMISSION_REQUEST_CODE);
                    } else {
                        callPlaceDetectionApi();
                    }

                }
            }
        });
    }



    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {
        Log.e(LOG_TAG, "Google Places API connection failed with error code: "
                + connectionResult.getErrorCode());

        Toast.makeText(this,
                "Google Places API connection failed with error code:" +
                        connectionResult.getErrorCode(),
                Toast.LENGTH_LONG).show();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode,
                                           String permissions[], int[] grantResults) {
        switch (requestCode) {
            case PERMISSION_REQUEST_CODE:
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    callPlaceDetectionApi();
                }
                break;
        }
    }

    private void callPlaceDetectionApi() throws SecurityException {

        Log.d("mvkvkmvkmvkm", "callPlaceDetectionApi: ");
        PendingResult<PlaceLikelihoodBuffer> result = Places.PlaceDetectionApi
                .getCurrentPlace(mGoogleApiClient, null);
        Log.d("result", result.toString());
        result.setResultCallback(new ResultCallback<PlaceLikelihoodBuffer>() {
            @Override
            public void onResult(PlaceLikelihoodBuffer likelyPlaces) {
                System.out.println("inside callback...");
                Status status = likelyPlaces.getStatus();
                System.out.println("Status.tostring"+status.toString());
                System.out.println(status.isSuccess());
                System.out.println(status.getStatusCode());
                System.out.println(status.getStatusMessage());
                System.out.println(status.getStatus());
                for (PlaceLikelihood placeLikelihood : likelyPlaces) {
                    Log.i(LOG_TAG, String.format("Place '%s' with " +
                                    "likelihood: %g",
                            placeLikelihood.getPlace().getName(),
                            placeLikelihood.getLikelihood()));
                }
                likelyPlaces.release();
            }
        });
    }




    @Override
    public void onStop() {
        super.onStop();


        mGoogleApiClient.disconnect();
    }

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

    }

    @Override
    public void onConnectionSuspended(int i) {

    }
}

Android 清单文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.******">
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".PlacesAPIActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

       <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version"/>

        <meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="A***************************o"/>
    </application>
</manifest>

日志屏幕

07-06 15:56:32.499 10925-10925/com.example.nearby D/connected1: true
07-06 15:56:32.500 10925-10925/com.example.nearby D/mvkvkmvkmvkm: callPlaceDetectionApi: 
07-06 15:56:32.554 10925-10925/com.example.nearby D/result: com.google.android.gms.location.places.internal.zzj$1@5dd8757
07-06 15:56:42.610 10925-10925/com.example.nearby I/System.out: inside callback...
07-06 15:56:42.610 10925-10925/com.example.nearby I/System.out: Status.tostringStatus{statusCode=ERROR, resolution=null}
07-06 15:56:42.610 10925-10925/com.example.nearby I/System.out: false
07-06 15:56:42.610 10925-10925/com.example.nearby I/System.out: 13
07-06 15:56:42.611 10925-10925/com.example.nearby I/System.out: ERROR
07-06 15:56:42.611 10925-10925/com.example.nearby I/System.out: Status{statusCode=ERROR, resolution=null}

【问题讨论】:

  • 你能解决这个问题吗?
  • 遇到同样的问题,你找到解决办法了吗?

标签: android google-places-api


【解决方案1】:

在获得大量此错误代码后,我解决了它在我的模拟器中发送我的 GPS 位置的问题。 在 Android Studio AVD - Android Emulator 中,我的程序正在运行,我单击“更多”>“位置”>“设置我的纬度和经度”,然后单击“发送”。 立即成功执行了对 getCurrentLocation() 的下一个请求。 您可以在执行应用程序之前将您的位置发送到模拟器。它也会起作用。

【讨论】:

    【解决方案2】:

    在我的情况下,如果不按照本示例 https://developers.google.com/places/android-api/current-places-tutorial 中所述初始化 Google 地图,Android 版 Google 地方信息将无法正常工作

    /**
     * Builds the map when the Google Play services client is successfully connected.
     */
    @Override
    public void onConnected(Bundle connectionHint) {
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }
    

    【讨论】:

      猜你喜欢
      • 2019-08-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-25
      • 2019-02-14
      • 2016-04-25
      相关资源
      最近更新 更多