gpt4 book ai didi

Android getLastKnownLocation(LocationManager.GPS_PROVIDER) 返回 null

转载 作者:太空狗 更新时间:2023-10-29 14:10:57 27 4
gpt4 key购买 nike

我正在使用以下代码来获取我当前的位置。它使用 NETWORK_PROVIDER 完美运行。但是每当我使用 GPS_PROVIDER 时,它总是返回 null。我知道同样的问题已被问过很多次,但没有一个能解决我的问题

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

// flag for GPS Status
boolean isGPSEnabled = false;

// flag for network status
boolean isNetworkEnabled = false;

boolean canGetLocation = false;

android.location.Location location;
double latitude;
double longitude;

// The minimum distance to change updates in metters
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10
// metters

// The minimum time beetwen updates in milliseconds
private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1
// minute

// Declaring a Location Manager
protected LocationManager locationManager;

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

public android.location.Location getLocation() {
try {
locationManager = (LocationManager) mContext
.getSystemService(LOCATION_SERVICE);

// getting GPS status
isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);

// getting network status
isNetworkEnabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

if (!isGPSEnabled && !isNetworkEnabled) {
// location service disabled
} else {
this.canGetLocation = true;

// if GPS Enabled get lat/long using GPS Services

if (isGPSEnabled) {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);

Log.d("GPS Enabled", "GPS Enabled");

if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
updateGPSCoordinates();
}
}

// First get location from Network Provider
if (isNetworkEnabled) {
if (location == null) {
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);

Log.d("Network", "Network");

if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
updateGPSCoordinates();
}
}

}
}
} catch (Exception e) {
// e.printStackTrace();
Log.e("Error : Location",
"Impossible to connect to LocationManager", e);
}

return location;
}

public void updateGPSCoordinates() {
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}

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

public void onLocationChanged(Location location) {
}

public void onProviderDisabled(String provider) {
}

public void onProviderEnabled(String provider) {
}

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

要从其他 Activity 中获取位置,我很简单
GPSTracker gps = new GPSTracker(this);
double lat = gps.latitude;
double lon = gps.longitude;

最佳答案

测试以下内容

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

// flag for GPS Status
boolean isGPSEnabled = false;

// flag for network status
boolean isNetworkEnabled = false;

boolean canGetLocation = false;

android.location.Location location;
double latitude;
double longitude;

// The minimum distance to change updates in metters
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 0;

// The minimum time beetwen updates in milliseconds
private static final long MIN_TIME_BW_UPDATES = 0;

// Declaring a Location Manager
protected LocationManager locationManager;

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

public android.location.Location getLocation() {
try {
locationManager = (LocationManager) mContext
.getSystemService(LOCATION_SERVICE);

// getting GPS status
isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);

// getting network status
isNetworkEnabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

if (!isGPSEnabled && !isNetworkEnabled) {
// location service disabled
} else {
this.canGetLocation = true;

// if GPS Enabled get lat/long using GPS Services

if (isGPSEnabled) {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);

Log.d("GPS Enabled", "GPS Enabled");

if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
//updateGPSCoordinates();
}
}

// First get location from Network Provider
if (isNetworkEnabled) {
if (location == null) {
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);

Log.d("Network", "Network");

if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
//updateGPSCoordinates();
}
}

}
}
} catch (Exception e) {
// e.printStackTrace();
Log.e("Error : Location",
"Impossible to connect to LocationManager", e);
}

return location;
}

public void updateGPSCoordinates(Location location) {
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}

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

public void onLocationChanged(Location location) {
Toast.makeText(getApplicationContext(), Ubicación Obtenida: " + location.toString(), Toast.LENGTH_LONG).show();
updateGPSCoordinates(Location location);
}

public void onProviderDisabled(String provider) {
}

public void onProviderEnabled(String provider) {
}

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

要获得LastKnowLocation,首先GPS 需要找到一个坐标,最好的方法是使用onLocationChanged 方法,因为是实时的,网络定位比建筑物内的gps 定位要快。您可以在 Toast 的帮助下查看 onLocationChanged 何时触发。之后,您可以在您正在构建的应用程序上检索 GPS 获取的最后一个位置。

关于Android getLastKnownLocation(LocationManager.GPS_PROVIDER) 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28458831/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com