显然我不是。以下是我用来获取位置的内容。
SingleShotLocationProvider.requestSingleUpdate(getContext(),
new SingleShotLocationProvider.LocationCallback() {
@Override
public void onNewLocationAvailable(SingleShotLocationProvider.GPSCoordinates location) {
//use location.longitude and location.latitude
}
});
这是我上面使用的课程:
public class SingleShotLocationProvider {
public interface LocationCallback {
void onNewLocationAvailable(GPSCoordinates location);
}
public static void requestSingleUpdate(final Context context, final LocationCallback callback) {
final LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
//If network is available
if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_COARSE);
try {
locationManager.requestSingleUpdate(criteria, new LocationListener() {
@Override
public void onLocationChanged(Location location) {
callback.onNewLocationAvailable(new GPSCoordinates(location.getLatitude(), location.getLongitude()));
}
@Override public void onStatusChanged(String provider, int status, Bundle extras) {}
@Override public void onProviderEnabled(String provider) {}
@Override public void onProviderDisabled(String provider) {}
}, null);
}
catch (SecurityException e){
Log.e("Location", "security exception");
}
}
//If GPS is enabled
else if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
try {
locationManager.requestSingleUpdate(criteria, new LocationListener() {
@Override
public void onLocationChanged(Location location) {
callback.onNewLocationAvailable(new GPSCoordinates(location.getLatitude(), location.getLongitude()));
}
@Override public void onStatusChanged(String provider, int status, Bundle extras) {}
@Override public void onProviderEnabled(String provider) {}
@Override public void onProviderDisabled(String provider) {}
}, null);
}
catch (SecurityException e){
Log.e("Location", "security exception");
}
}
else{
Toast.makeText(context, "No network or GPS available. Try again later.", Toast.LENGTH_LONG).show();
}
}
// consider returning Location instead of this dummy wrapper class
public static class GPSCoordinates {
public float longitude = -1;
public float latitude = -1;
public GPSCoordinates(float lon, float lat) {
longitude = lon;
latitude = lat;
}
public GPSCoordinates(double lon, double lat) {
longitude = (float) lon;
latitude = (float) lat;
}
}
}
任何人对这里发生的事情都有任何想法?此外,出于某种原因,我只能在启用GPS时获取位置,而不是通过我的网络/ wifi。如果我尝试禁用GPS,即使我有良好的网络连接,if和elif条件都会失败。想法?
答案 0 :(得分:1)
您正在以错误的顺序传递参数。在GPSCoordinates
的构造函数上,您希望经度和纬度按此顺序排列。当您创建GPSCoordinates
时,您会以相反的顺序传递纬度和经度。
将您的GPSCoordinates
更改为:
public static class GPSCoordinates {
public float longitude = -1;
public float latitude = -1;
public GPSCoordinates(float lat, float lon) {
latitude = lat;
longitude = lon;
}
public GPSCoordinates(double lat, double lon) {
latitude = (float) lat;
longitude = (float) lon;
}
}