如何在gps位置启用后立即获取位置

时间:2018-04-23 08:13:30

标签: android android-location android-maps

我正在实施一个模块,该模块需要用户纬度经度才能离线打卡。

我已经在LINK

上按照此示例实施了 GPSTracker

但启用GPS位置后,我正在打击出勤,然后此类返回null位置对象。但是在 30 60 秒之后它会正确返回位置对象。

我还在Manifest中添加了COARSEFINE权限,并获得了运行时权限。

  

所以我需要帮助如何立即获得经度和经度   启用GPS定位。

3 个答案:

答案 0 :(得分:3)

启用GPS位置mFusedLocationClient.getLastLocation()后,返回null,因为它没有lastLocation。如果您需要在启用后立即获取位置,则需要进行self requestLocationUpdate之类的

mFusedLocationClient.requestLocationUpdates(getLocationRequest(), mLocationCallback, null);

来自com.google.android.gms.location.LocationCallback的mLocationCallback;

并在回调中获得结果,如:

mLocationCallback = new LocationCallback() {
        @SuppressLint("MissingPermission")
        @Override
        public void onLocationResult(LocationResult locationResult) {
            if (locationResult == null) {
                mFusedLocationClient.removeLocationUpdates(mLocationCallback);
                return;
            }
            for (Location location : locationResult.getLocations()) {
                // Update UI with location data
                myCoordinate = new LatLng(location.getLatitude(), location.getLongitude());
            }
            mFusedLocationClient.removeLocationUpdates(mLocationCallback);
        }
    };

PS。就我而言,它可以在所有设备上正常运行,但不能在三星s8和s9上运行

答案 1 :(得分:0)

试试这个:

FusedLocationProviderClient mFusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(getActivity());

private void getDeviceLocation() {   
            try {
                Task<Location> locationResult = mFusedLocationProviderClient.getLastLocation();
                locationResult.addOnCompleteListener(getActivity(), new OnCompleteListener<Location>() {
                    @Override
                    public void onComplete(@NonNull Task<Location> task) {
                        if (task.isSuccessful()) {                 
                            Location mLastKnownLocation = task.getResult();
                             Log.i(TAG, "==lat "+mLastKnownLocation.getLatitude());
                             Log.i(TAG, "==log "+mLastKnownLocation.getLongitude());                      
                        } else {
                            Log.d(TAG, "Current location is null. Using defaults.");
                            Log.e(TAG, "Exception: %s", task.getException());
                        }
                    }
                });
            } catch (SecurityException e) {
                Log.e("Exception: %s", e.getMessage());
            }
     }

答案 2 :(得分:0)

此代码在GPS开启后立即生效

private void getLastLocation()
{
    LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);


    LocationListener locationListener = new LocationListener() {
        public void onLocationChanged(Location location) {
            Log.e("location",location.getLatitude()+"");

        }

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

        public void onProviderEnabled(String provider) {}

        public void onProviderDisabled(String provider) {}
    };

    locationManager.requestSingleUpdate(LocationManager.NETWORK_PROVIDER,locationListener,null);

}