使用FusedLocationApi查找位置时出现Google Api错误

时间:2018-04-10 18:23:39

标签: java android android-studio

这是我的跟踪类,我已经导入了相关的依赖项。

 public class TrackingOrder extends FragmentActivity implements      OnMapReadyCallback,
    GoogleApiClient.ConnectionCallbacks,
    GoogleApiClient.OnConnectionFailedListener,
    LocationListener{

private GoogleMap mMap;

private final static int PLAY_SERVICES_RESOLUTION_REQUEST = 1000;
private final static int LOCATION_PERMISSION_REQUEST = 1001;

private Location mLastLocation;

private GoogleApiClient mGoogleApiClient;
private LocationRequest mLocationRequest;

private static int UPDATE_INTERVAL = 1000;
private static int FATEST_INTERVAL = 5000;
private static int DISPLACEMENT = 10;


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

    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
            && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)
    {
        requestRuntimePermission();
    }

    else
    {
        if(checkPlayServices())
        {
            buildGoogleApiClient();
            createLocationRequest();
        }
        displayLocation();

    }
    displayLocation();
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
}

private void displayLocation() {
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
            && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)
    {
        requestRuntimePermission();
    }
    else
    {
        mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
        if(mLastLocation != null)
        {
            double latitude = mLastLocation.getLatitude();
            double longitude = mLastLocation.getLongitude();

            //Adding Marker to location
            LatLng yourLocation = new LatLng(latitude,longitude);
            mMap.addMarker(new MarkerOptions().position(yourLocation).title("Your Location"));
            mMap.moveCamera(CameraUpdateFactory.newLatLng(yourLocation));
            mMap.animateCamera(CameraUpdateFactory.zoomTo(17.0f));

        }
        else
        {
            Toast.makeText(this, "Couldn't Get Location", Toast.LENGTH_SHORT).show();
        }
    }

}

private void createLocationRequest() {
    mLocationRequest = new LocationRequest();
    mLocationRequest.setInterval(UPDATE_INTERVAL);
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    mLocationRequest.setSmallestDisplacement(DISPLACEMENT);

}

private void requestRuntimePermission() {
    ActivityCompat.requestPermissions(this, new String[]
            {
                    Manifest.permission.ACCESS_COARSE_LOCATION,
                    Manifest.permission.ACCESS_FINE_LOCATION,

            }, LOCATION_PERMISSION_REQUEST);
}

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

}

@Override
public void onLocationChanged(Location location) {
    mLastLocation = location;
    displayLocation();

}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    switch (requestCode) {
        case LOCATION_PERMISSION_REQUEST:
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                if (checkPlayServices())
                {
                    buildGoogleApiClient();
                    createLocationRequest();
                }
            }
            break;
    }
}

protected synchronized void buildGoogleApiClient() {
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API).build();


    mGoogleApiClient.connect();

}

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(this, "This Device is not Supported", Toast.LENGTH_SHORT).show();
            finish();
        }
        return false;

    }
    return true;
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {

}

@Override
public void onProviderEnabled(String provider) {

}

@Override
public void onProviderDisabled(String provider) {

}

@Override
public void onConnected(@Nullable Bundle bundle) {
    displayLocation();
    startLocationUpdates();

}

private void startLocationUpdates() {
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
            && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        return;
    }
    LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
}

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

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

}

@Override
protected void onResume() {
    super.onResume();
    checkPlayServices();
}

@Override
protected void onStart() {
    super.onStart();
    if(mGoogleApiClient != null)
        mGoogleApiClient.connect();
}

}

我已将FusedLocationApi更改为FusedLocatinProviderClient,但仍显示错误。

当我输入FusedLocationProviderClient时,它出现红色并且也无法正常工作。我收到了错误。

由于此问题,我无法在我的应用程序中找到用户位置。

0 个答案:

没有答案