在android中获取纬度和经度程序总是获取GPS LastKnown位置空值

在android中获取纬度和经度程序总是获取GPS LastKnown位置空值,android,gps,Android,Gps,大家好,我知道这个问题太老了,有很多问题,但没有一个解决方案适合我。我正在获取纬度和经度程序。我尝试了下面的代码,但每次获取LastKnownLocation=null时,GPS都会发生变化。此外,我的GPS在我的设备上已启用,我在不同的设备上尝试了这段代码,在其中一些设备上,我可以从GPS获得纬度和经度,但对于大多数设备,它显示为空。我不知道为什么这段代码在大多数情况下都失败了,如果你们中的任何人知道这方面的任何事情或更好的方法,那么请告诉我,我已经花了将近一周的时间来挖掘上面代码中的错误,但

大家好,我知道这个问题太老了,有很多问题,但没有一个解决方案适合我。我正在获取纬度和经度程序。我尝试了下面的代码,但每次获取LastKnownLocation=null时,GPS都会发生变化。此外,我的GPS在我的设备上已启用,我在不同的设备上尝试了这段代码,在其中一些设备上,我可以从GPS获得纬度和经度,但对于大多数设备,它显示为空。我不知道为什么这段代码在大多数情况下都失败了,如果你们中的任何人知道这方面的任何事情或更好的方法,那么请告诉我,我已经花了将近一周的时间来挖掘上面代码中的错误,但没有任何帮助

更新-谷歌的FusedLocationApi是免费的还是每天的请求有限??

public class GPSTracker extends Service implements LocationListener {
private final Context context;

boolean isGPSEnabled = false;
boolean isNetworkEnabled = false;
boolean canGetLocation = false;

Location location;

double latitude;
double longitude;

private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10;
private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1;

protected LocationManager locationManager;

public GPSTracker(Context context) {
    this.context = context;
    getLocation();
}

public Location getLocation() {
    try {
        locationManager = (LocationManager) context.getSystemService(LOCATION_SERVICE);
        isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

        if(!isGPSEnabled && !isNetworkEnabled) {

        } else {
            this.canGetLocation = true;
            if (isGPSEnabled) {
                Log.d("gps", "gpsenabled");
                if (location == null) {
                    Log.d("gps", "location is null");
                    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                    if (locationManager != null) {
                        Log.d("gps", "locationManager is not null");
                        location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                        if (location != null) {
                            Log.d("gps", "location is not null");
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                            Log.d("gps", "lat: " + latitude + ", lon: " + longitude);
                        }
                    }
                }
            }
            if (isNetworkEnabled) {
                Log.d("gps", "networkenabled");
                locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                if (locationManager != null) {
                    Log.d("gps", "locationManager is not null");
                    location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                    if (location != null) {
                        Log.d("gps", "location is not null");
                        latitude = location.getLatitude();
                        longitude = location.getLongitude();
                        Log.d("gps", "lat: " + latitude + ", lon: " + longitude);
                    }
                }
            }
            //
            LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

            Criteria criteria = new Criteria();
            String bestProvider = lm.getBestProvider(criteria, false);
            Log.d("gps", "bestProvider: " + bestProvider);
            Location location = lm.getLastKnownLocation(bestProvider);
            Log.d("gps", "lat: " + location.getLatitude() + ", lon: " + location.getLongitude());
            //
        }
    } catch (Exception e) {

    }
    return location;
}

public void stopUsingGPS() {
    if (locationManager != null) {
        locationManager.removeUpdates(GPSTracker.this);
    }
}

public double getLatitude() {
    if (location != null) {
        latitude = location.getLatitude();
    }
    return latitude;
}

public double getLongitude() {
    if (location != null) {
        latitude = location.getLongitude();
    }
    return longitude;
}

public boolean canGetLocation() {
    return this.canGetLocation;
}

public void showSettingsAlert() {
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
    alertDialog.setTitle("GPS is settings");
    alertDialog.setMessage("GPS is not enabled. Do you want to go to setttings menu ?");
    alertDialog.setPositiveButton("Setting", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            context.startActivity(intent);
        }
    });
    alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
        }
    });
    alertDialog.show();
}

@Override
public void onLocationChanged(Location location) {
    this.location = location;
    latitude = getLatitude();
    longitude = getLongitude();

    Log.d("gps", "onLocationChanged");
}

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

}

@Override
public void onProviderEnabled(String provider) {

}

@Override
public void onProviderDisabled(String provider) {

}

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

请结帐我在这里回答了请结帐我在这里回答了