GoogleFusedLocation api为lat返回null,为lon和altitude返回0.0

时间:2017-06-30 17:08:24

标签: android google-maps geolocation location fusedlocationproviderapi

很抱歉长标题但无法恢复更多,基本上我实施googlefusedlocation api作为服务,我需要服务在后台运行并更新位置,此服务启动我的应用程序运行(atm我是没有请求用户,如果他想分享位置,因为我不知道如何使用服务实现它。

我的服务:

package com.example.afcosta.inesctec.pt.android.services;

import android.Manifest;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationManager;
import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.util.Log;

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


public class GoogleLocation extends Service implements GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener,
        LocationListener {

    private static final String TAG = "BOOMBOOMTESTGPS";
    private static final long FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS = 0;
    private LocationManager mLocationManager = null;
    private static final int LOCATION_INTERVAL = 0;
    private static final float LOCATION_DISTANCE = 0f;
    private int updatePriority;
    private GoogleApiClient googleApiClient;
    private LocationRequest locationRequest;

    private final IBinder mBinder = new LocalBinder();
    private Intent intent;
    private String provider;
    Context context;


    Location mLastLocation;

    public class LocalBinder extends Binder {
        public GoogleLocation getServerInstance() {
            return GoogleLocation.this;
        }
    }

    public Location getLocation() {
        Log.d("IMHERE", "HELLO");
        return mLastLocation;
    }


    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.e(TAG, "onStartCommand");
        context = getApplicationContext();
        super.onStartCommand(intent, flags, startId);
        return START_STICKY;
    }


    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }


    @Override
    public void onCreate() {
        Log.e(TAG, "onCreate");
        initializeLocationManager();

        if (mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
            this.updatePriority = LocationRequest.PRIORITY_HIGH_ACCURACY;
        } else if (mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
            this.updatePriority = LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY;
        } else {
            this.updatePriority = LocationRequest.PRIORITY_HIGH_ACCURACY;
        }

        this.buildGoogleApiClient();
        this.createLocationRequest();
        this.googleApiClient.connect();

    }

    @Override
    public void onConnected(@Nullable Bundle bundle) {
        if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) ==
                PackageManager.PERMISSION_GRANTED &&
                ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) ==
                        PackageManager.PERMISSION_GRANTED) {
            LocationServices.FusedLocationApi.requestLocationUpdates(this.googleApiClient, this.locationRequest,this);

        } else {
            ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, 1);
        }
    }

    @Override
    public void onConnectionSuspended(int i) {

    }

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

    }

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


    @Override
    public void onDestroy() {
        Log.e(TAG, "onDestroy");
        super.onDestroy();

        this.googleApiClient.unregisterConnectionCallbacks(this);
        this.googleApiClient.unregisterConnectionFailedListener(this);
        this.googleApiClient.disconnect();
        this.mLastLocation = null;
    }



    private void initializeLocationManager() {
        Log.e(TAG, "initializeLocationManager");
        mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
        if (mLocationManager == null) {
            mLocationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
        }
    }

    private synchronized void buildGoogleApiClient() {
        this.googleApiClient = new GoogleApiClient.Builder(this)
                .addApi(LocationServices.API)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();
    }

    private void createLocationRequest() {
        this.locationRequest = new LocationRequest();
        this.locationRequest.setInterval(FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS);
        this.locationRequest.setPriority(updatePriority);
    }

然后在我的相机上我访问我服务中的getLocation方法,它给了我最后的位置,我只是显示我访问服务的方式,如果你们问,但我只记得我之前有一个解决方案(我使用locationListener,而不是谷歌一个),我可以得到位置,有时失败,这就是为什么我试图改变。

 public void onServiceConnected(ComponentName name, IBinder service) {
        mBounded = true;
        GoogleLocation.LocalBinder mLocalBinder = (GoogleLocation.LocalBinder)service;
        mlocation = mLocalBinder.getServerInstance();
        location = mlocation.getLocation();
        Log.d("localizacao",location.toString());
    }

现在我明白了:

纬度:空 LON:0.0 ALT:0.0

奇怪,为什么会发生这种情况?

由于

0 个答案:

没有答案