无Gps

时间:2016-09-07 20:05:18

标签: android android-fusedlocation

我正在尝试使用FusedLocationApi使用以下代码获取用户当前位置。我按照此处提供的指南进行操作:https://developer.android.com/training/location/retrieve-current.html

到目前为止我已尝试过这个:

package com.abhishek.locationawareapp;

import android.Manifest;
import android.content.pm.PackageManager;
import android.location.Location;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;

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

    private GoogleApiClient mGoogleApiClient;
    LocationRequest mLocationRequest;
    protected void createLocationRequest(){
        mLocationRequest = new LocationRequest();
        mLocationRequest.setInterval(10000);
        mLocationRequest.setFastestInterval(5000);
        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.d("Main Activity: ","onCreate");
        if (mGoogleApiClient == null) {
            mGoogleApiClient = new GoogleApiClient.Builder(this)
                    .addConnectionCallbacks(this)
                    .addOnConnectionFailedListener(this)
                    .addApi(LocationServices.API)
                    .build();//.addApi(LocationServices.class)
        }
        //For Location Request
        /*LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
                .addLocationRequest(mLocationRequest);*/


    }

    @Override
    protected void onStart() {
        mGoogleApiClient.connect();
        Log.d("Main Activity: ","onStart");
        super.onStart();
    }

    @Override
    protected void onStop() {
        Log.d("Main Activity: ","onStop");
        mGoogleApiClient.disconnect();
        super.onStop();
    }

    @Override
    public void onConnected(@Nullable Bundle bundle) {
        Log.d("Main Activity: ","onConnected");
        if(Build.VERSION.SDK_INT >=23){
            checkLocationPermission();
        }else{
            locationPermission = true;
            Location mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
            if(mLastLocation !=null){
                String mLatitudeText = String.valueOf(mLastLocation.getLatitude());
                String mLongitudeText = String.valueOf(mLastLocation.getLongitude());
                Log.d("Location: ","mLatitudeText: "+ mLatitudeText +" mLongitudeText: "+ mLongitudeText);
            }
        }

    }
    boolean locationPermission = false;
    final static int LOC_REQ_CODE = 100;
    private void checkLocationPermission(){
        int locationPermissionCheck = ActivityCompat.checkSelfPermission(this,Manifest.permission.ACCESS_FINE_LOCATION);
        if(locationPermissionCheck == PackageManager.PERMISSION_GRANTED){
            locationPermission = true;
            Location mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
            if(mLastLocation !=null){
                String mLatitudeText = String.valueOf(mLastLocation.getLatitude());
                String mLongitudeText = String.valueOf(mLastLocation.getLongitude());
                Log.d("Location: ","mLatitudeText: "+ mLatitudeText +" mLongitudeText: "+ mLongitudeText);
            }
        }else{
            locationPermission = false;
            ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                    LOC_REQ_CODE);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode){
            case LOC_REQ_CODE:
                if(grantResults.length>0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
                    locationPermission = true;
                    Location mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
                    if(mLastLocation !=null){
                        String mLatitudeText = String.valueOf(mLastLocation.getLatitude());
                        String mLongitudeText = String.valueOf(mLastLocation.getLongitude());
                        Log.d("Location: ","mLatitudeText: "+ mLatitudeText +" mLongitudeText: "+ mLongitudeText);
                    }
                }else{
                    final Snackbar snackBar = Snackbar.make(findViewById(R.id.rl_layout), "Location access is required to get last location.",
                            Snackbar.LENGTH_INDEFINITE);

                    snackBar.setAction("Dismiss", new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            checkLocationPermission();
                            snackBar.dismiss();
                        }
                    });
                    snackBar.show();
                }
                break;
        }
    }

    @Override
    public void onConnectionSuspended(int i) {
        Log.d("Main Activity: ","onConnectionSuspended");
    }

    @Override
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
        Log.d("Main Activity: ","onConnectionFailed");
    }
}

我获得了启用gps的位置但是一旦我禁用了gps,mLastLocation 始终返回null。是否可以在不使用Gps的情况下从FusedLocationApi获取位置?请帮忙!!

0 个答案:

没有答案