当前位置: 首页 > 知识库问答 >
问题:

在android中快速一次获取当前位置

颜骁
2023-03-14

谁能给我建议一种简单快速的方法,只需一次就能得到位置?!

共有1个答案

白翰海
2023-03-14

给,你可以用这个...

用法示例:

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());
     }
   });
}

您可能希望验证lat/long是实际值,而不是0或其他值。如果我没记错的话,这不应该抛出一个NPE,但是您可能想要验证这一点。

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定位系统的NETWORK提供程序获取当前位置坐标时遇到麻烦。 已经阅读了很多教程,并为我的项目实现了4到5个现有的类,所有这些类都给了我最后的坐标,而不是当前的坐标。 我很确定这个问题是我遗漏的根本问题,但是我无法理解到底是什么。 我现在正在使用的代码: 这是我的主要活动 这是我用于跟踪的课程: 这是我的AndroidManifest.xml 实际上,GPS定位工

  • 问题内容: 我正在尝试通过GPS功能获取用户的当前位置, 编写了一个实现的简单类 通过一个简单的动作,我正在访问这些经度和纬度值 但是它总是返回0.0作为结果。无法找出问题所在。 问题答案: 您必须在onCreate()返回之后才能触发您的位置更新回调。如果您将经纬度变量初始化为虚拟值,则可能会看到您正在打印这些值。 在onLocationChanged中添加一些日志记录,以便可以看到它已被触发,

  • 我试图实现当前位置地图应用程序,我正在加载地图碎片,但它需要纬度和经度值来定位地址...如何实现当前位置地图功能...提前感谢.. 这是我的地图片段。

  • 随着被弃用,我很难使用在单击按钮后仅一次获得用户的位置。

  • 问题内容: 在我的应用程序中,我在打开应用程序时获得了当前位置的纬度和经度,但是在关闭应用程序时却没有得到。 我正在使用Service类在应用程序中获取当前位置的纬度和经度。 请告诉我即使关闭应用程序也如何获取当前位置的纬度和经度 问题答案: 几个月前,我创建了GPSTracker库来帮助我获取GPS位置。如果您需要查看GPSTracker> getLocation AndroidManifest