融合位置提供商问题

时间:2017-08-06 11:39:01

标签: android fusedlocationproviderapi android-fusedlocation

我正在使用Fused Location Api来获取纬度和经度。文档中提到它使用GPS,Wifi和网络(Cell)来返回用户的最准确位置。我在这里提出了几个问题:

1)我注意到如果我关闭wifi,它仍然会给出结果,但是当GPS关闭时它会为空,为什么会这样?即使gps关闭或不可用,它是否假设使用Wifi来提供位置细节?附:我已使用ACCESS_FINE_LOCATION获得许可。

2)我如何知道/测试是否使用Wifi或GPS或网络(手机信号塔)返回位置详细信息?

3)当GPS和wifi关闭时,它是否可以使用网络(手机信号塔)?

融合位置提供商代码

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    lblLocation = (TextView) findViewById(R.id.lblLocation);
    btnShowLocation = (Button) findViewById(R.id.btnShowLocation);
    btnStartLocationUpdates = (Button) findViewById(R.id.btnLocationUpdates);

    if (checkPlayServices()) {
        Log.i("playservice", checkPlayServices()+"");
        // Building the GoogleApi client
        buildGoogleApiClient();
    }

    btnShowLocation.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            displayLocation();
        }
    });

}

private void displayLocation() {

    mLastLocation = LocationServices.FusedLocationApi
            .getLastLocation(mGoogleApiClient);

    if (mLastLocation != null) {
        double latitude = mLastLocation.getLatitude();
        double longitude = mLastLocation.getLongitude();

        lblLocation.setText(latitude + ", " + longitude);
    } else {
        lblLocation.setText("(Couldn't get the location. Make sure location is enabled on the device)");
    }
}

protected synchronized void buildGoogleApiClient() {
    Log.i(TAG, "Building GoogleApiClient");

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

private boolean checkPlayServices() {
    int resultCode = GooglePlayServicesUtil
            .isGooglePlayServicesAvailable(this);
    if (resultCode != ConnectionResult.SUCCESS) {
        if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) {
            GooglePlayServicesUtil.getErrorDialog(resultCode, this,
                    PLAY_SERVICES_RESOLUTION_REQUEST).show();
        } else {
            Toast.makeText(getApplicationContext(),
                    "This device is not supported.", Toast.LENGTH_LONG)
                    .show();
            finish();
        }
        return false;
    }
    return true;
}

@Override
public void onConnected(Bundle bundle) {

    Log.i(TAG, "GoogleApiClient connected!");
    createLocationRequest();
    mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
    Log.i(TAG, " Location: " + mLastLocation);
}

protected void createLocationRequest() {

    mLocationRequest = new LocationRequest();
    mLocationRequest.setInterval(10000);
    mLocationRequest.setFastestInterval(5000);
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, new LocationCallback() {
        @Override
        public void onLocationResult(final LocationResult locationResult) {
            Log.i("onLocationResult",locationResult + "");

            btnShowLocation.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    lblLocation.setText(locationResult.getLocations() + "");
                }
            });
        }

        @Override
        public void onLocationAvailability(LocationAvailability locationAvailability) {
            Log.i("onLocationAvailability", "onLocationAvailability: isLocationAvailable =  " + locationAvailability.isLocationAvailable());
        }
    }, null);
}

@Override
public void onConnectionSuspended(int i) {
    mGoogleApiClient.connect();
}

@Override
public void onConnectionFailed(ConnectionResult result) {
    Log.i(TAG, "Connection failed: ConnectionResult.getErrorCode() = "
            + result.getErrorCode());
}


@Override
protected void onStart() {
    super.onStart();

    if (mGoogleApiClient != null) {
        mGoogleApiClient.connect();
        Log.i(TAG, "mGoogleApiClient.connect()");
    }
}

当我打开GPS时,我得到的输出是:

I/onLocationResult(7481): LocationResult[locations: [Location[fused 27.673576,85.314083 acc=16 et=+1d1h59m30s347ms alt=1289.0999755859375 vel=0.93 bear=166.0]]]

0 个答案:

没有答案