在android中快速获取当前位置 [英] get the current location fast and once in android

查看:124
本文介绍了在android中快速获取当前位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需要设备当前位置(纬度和经度)的android应用程序。我在网上尝试了一些教程,特别是一些堆栈溢出的解决方案,但它们对我来说效果不佳。我的要求非常简单:首先我需要它快速,并且在片段开始时需要一次位置。第二我需要它尽可能精确,我的意思是它应该首先使用GPS,如果GPS不可用,然后使用网络提供商。



例如,我试过这个解决方案,但它在30秒后返回null,但我知道有一些所有的东西都可以,因为谷歌地图和其他应用程序运行良好!

几乎所有答案都建议使用getLastKnownLocation(),但我认为它不是当前的,如果是这样的话,我不希望它。 p>

任何人都可以向我推荐某种简单而快速的方法来获取位置ONCE?!



预先感谢

解决方案

在这里,您可以使用这个...



用法:

  public void foo(Context context){
//当您需要位置时
//如果内部活动上下文= this;

SingleShotLocationProvider.requestSingleUpdate(context,
)SingleShotLocationProvider.LocationCallback(){
@Override public void onNewLocationAvailable(GPSCoordinates location){
Log.d(Location ,我的位置是+ location.toString());
}
});
}

您可能想验证纬度/经度是实际值而不是0或一些东西。如果我没有记错,这不应该抛出一个NPE,但你可能想验证。

  public class SingleShotLocationProvider {

public static interface LocationCallback {
public void onNewLocationAvailable(GPSCoordinates位置);
}

//回调到调用线程,注意这是为了低谷物:如果你想要更高的精度,交换
// else和if的内容。另外一定要检查gps权限/设置是否允许。
//调用通常需要< 10ms
public static void requestSingleUpdate(final Context context,final LocationCallback callback){
final LocationManager locationManager =(LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
boolean isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if(isNetworkEnabled){
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_COARSE);
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);
} else {
boolean isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
if(isGPSEnabled){
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
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);
}
}
}


//考虑返回Location而不是这个虚拟包装类
public static class GPSCoordinates {
公共浮点经度= -1;
public float latitude = -1;

public GPSCoordinates(float theLatitude,float theLongitude){
longitude = theLongitude;
latitude = theLatitude;
}

public GPSCoordinates(doubleLayout){
longitude =(float)theLongitude;
latitude =(float)theLatitude;
}
}
}


I have an android application that need device current location (latitude and longitude). I've tried some tutorial on the net and specially some solutions from stack overflow, but they doesn't work well for me. My requirement is so simple: First I need it to be fast and need the location once when I the fragment starts. Second I need it to be as precise as possible, I mean it should use GPS first if GPS is not available then use Network provider.

For example, I've tried this solution but it return null after 30 second, but I know there is some all the things is ok because google map and other application works well !!!

Something that almost all the answers suggest is to use getLastKnownLocation(), but I suppose it's not the current and I don't want it if it is so.

can anyone suggest me some kind of simple and fast way to get the location just ONCE ?!

Thanks in advance

解决方案

Here, you can use this...

Example usage:

public void foo(Context context) {
  // when you need location
  // if inside activity context = this;

  SingleShotLocationProvider.requestSingleUpdate(context, 
   new SingleShotLocationProvider.LocationCallback() {
     @Override public void onNewLocationAvailable(GPSCoordinates location) {
       Log.d("Location", "my location is " + location.toString());
     }
   });
}

You might want to verify the lat/long are actual values and not 0 or something. If I remember correctly this shouldn't throw an NPE but you might want to verify that.

public class SingleShotLocationProvider {

  public static interface LocationCallback {
      public void onNewLocationAvailable(GPSCoordinates location);
  }

  // calls back to calling thread, note this is for low grain: if you want higher precision, swap the 
  // contents of the else and if. Also be sure to check gps permission/settings are allowed.
  // call usually takes <10ms
  public static void requestSingleUpdate(final Context context, final LocationCallback callback) {
      final LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
      boolean isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
      if (isNetworkEnabled) {
          Criteria criteria = new Criteria();
          criteria.setAccuracy(Criteria.ACCURACY_COARSE);
          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);
      } else {
          boolean isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
          if (isGPSEnabled) {
              Criteria criteria = new Criteria();
              criteria.setAccuracy(Criteria.ACCURACY_FINE);
              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);
          }
      }
  }


  // consider returning Location instead of this dummy wrapper class
  public static class GPSCoordinates {
      public float longitude = -1;
      public float latitude = -1;

      public GPSCoordinates(float theLatitude, float theLongitude) {
          longitude = theLongitude;
          latitude = theLatitude;
      }

      public GPSCoordinates(double theLatitude, double theLongitude) {
          longitude = (float) theLongitude;
          latitude = (float) theLatitude;
      }
  }  
}

这篇关于在android中快速获取当前位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆