位置为null Fused location provider android

时间:2017-02-13 17:26:23

标签: android location fusedlocationproviderapi android-fusedlocation

我正在检查是否在移动设备上启用了位置服务,如果是,则侦听构建googleapiclient并收听位置更改。我面临的问题是,一旦在移动和移动的位置被转动,我无法获得位置(纬度/长度)。该位置始终为null

isLocationEnabled - 检查位置是否已开启

            if (isLocationEnabled(getApplicationContext())) {
                mMenu.getItem(0).setVisible(true);
            }
            else {
                Snackbar.make(mLayout, getString(R.string.loc_not_enable),
                        Snackbar.LENGTH_LONG).show();
            }
        }
    });
    //setup GoogleApiclient
    buildGoogleApiClient();


 public static boolean isLocationEnabled(Context context) {
    int locationMode = 0;
    String locationProviders;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
        try {
            locationMode = Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.LOCATION_MODE);
        } catch (SettingNotFoundException e) {
            e.printStackTrace();
            return false;
        }
        return locationMode != Settings.Secure.LOCATION_MODE_OFF;

    }else{
        locationProviders = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
        return !TextUtils.isEmpty(locationProviders);
    }
}


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

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

@Override
protected void onStop() {
    // Disconnecting the client invalidates it.
    mGoogleApiClient.disconnect();
    super.onStop();
}

@Override
public void onConnected(Bundle bundle) {
    if (ContextCompat.checkSelfPermission(getApplicationContext(), android.Manifest.permission.ACCESS_FINE_LOCATION)
            != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, PERMISSION_LOCATION);
    } else {
        startLocationServices();

    }
}

private void startLocationServices() {
    try {
        LocationRequest mLocationRequest = LocationRequest.create();
        Location mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
                mGoogleApiClient);
        if (mLastLocation==null){
            LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
        } else {
            Log.d("mLastLocation", String.valueOf(mLastLocation));
            lat = String.valueOf(mLastLocation.getLatitude());
            lon = String.valueOf(mLastLocation.getLongitude());
        }
        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        mLocationRequest.setInterval(10*1000); // Update location every 10 seconds
        mLocationRequest.setFastestInterval(1 * 1000);
        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
    } catch (SecurityException exception) {
        exception.printStackTrace();
    }
}


   E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.my.game.wesport, PID: 30629
              java.lang.NumberFormatException: empty String
                  at java.lang.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1071)
                  at java.lang.Double.parseDouble(Double.java:547)
                  at com.my.game.wesport.MapsActivity.onMapReady(MapsActivity.java:161)
                  at com.google.android.gms.maps.SupportMapFragment$zza$1.zza(Unknown Source)
                  at com.google.android.gms.maps.internal.zzt$zza.onTransact(Unknown Source)
                  at android.os.Binder.transact(Binder.java:499)
                  at zu.a(:com.google.android.gms.DynamiteModulesB:82)
                  at maps.ad.t$5.run(Unknown Source)
                  at android.os.Handler.handleCallback(Handler.java:751)
                  at android.os.Handler.dispatchMessage(Handler.java:95)
                  at android.os.Looper.loop(Looper.java:154)
                  at android.app.ActivityThread.main(ActivityThread.java:6077)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

1 个答案:

答案 0 :(得分:0)

重新启动手机后问题得以解决。