我使用以下代码来获取当前位置。它使用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;
}
}
要从其他活动中获取位置,我就这么做了
GPSTracker gps = new GPSTracker(this);
double lat = gps.latitude;
double lon = gps.longitude;
答案 0 :(得分:1)
测试以下
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位置快。你可以看到onLocationChanged是在吐司的帮助下解雇的。之后,您可以检索您正在构建的应用程序上的GPS获取的最后位置。
答案 1 :(得分:0)
也许GPS没有抓住一个坐标,在方法“onLocationChanged”你可以控制它,知道它的最好方法,就是越过你正在测试的建筑物,看看发生了什么,如果你想要拿走一秒钟之后,GPS捕获来自卫星的坐标。之后。
public void onLocationChanged(Location location) {
this.location = location;
updateGPSCoordinates();
}
答案 2 :(得分:0)
只需更改一行
location = locationManager .getLastKnownLocation(LocationManager.GPS_PROVIDER);到
location = locationManager .getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);