【问题标题】:How does it work - requestLocationUpdates() + LocationRequest/Listener它是如何工作的 - requestLocationUpdates() + LocationRequest/Listener
【发布时间】:2013-05-29 17:14:10
【问题描述】:

我是新的 Android 编码员,我在请求本地化更新时遇到问题。

我正在使用来自 http://developer.android.com/training/location/receive-location-updates.html 的教程。

我的应用程序可以处理异常,正确获取纬度和经度,地理编码器可以处理显示地址。但我只要求位置一次 - 或者当位置发生变化时。我想做时间间隔。现在我开始实现教程中的代码,它看起来像这样:

public class MainActivity extends Activity implements 
GooglePlayServicesClient.ConnectionCallbacks,
GooglePlayServicesClient.OnConnectionFailedListener,
LocationListener {

private static final int MILLISECONDS_PER_SECOND = 1000;

public static final int UPDATE_INTERVAL_IN_SECONDS = 5;
private static final long UPDATE_INTERVAL =
          MILLISECONDS_PER_SECOND * UPDATE_INTERVAL_IN_SECONDS;

private static final int FASTEST_INTERVAL_IN_SECONDS = 1;
private static final long FASTEST_INTERVAL =
          MILLISECONDS_PER_SECOND * FASTEST_INTERVAL_IN_SECONDS;

private TextView tvStatus;
private TextView tvLatitude;
private TextView tvLongitude;

LocationRequest mLocationRequest;
LocationClient mLocationClient;
Location mCurrentLocation;

boolean bNetworkEnabled;
boolean bGPSEnabled;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);
    tvStatus = (TextView)findViewById(R.id.tvStatus);
    tvLatitude = (TextView)findViewById(R.id.tvLatitude);
    tvLongitude = (TextView)findViewById(R.id.tvLongitude);

    mLocationRequest = LocationRequest.create();
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    mLocationRequest.setInterval(UPDATE_INTERVAL);
    mLocationRequest.setFastestInterval(FASTEST_INTERVAL);
    mLocationClient = new LocationClient(this, this, this);

    checkProviders();
}

因此已经实施了间隔和位置请求。但是在我之前给出的链接中有一条评论说我应该在某处使用requestLocationUpdates()(可能是onCreate()onStart() 和删除onStop() 上的请求),但我有问题。所以,Eclipse 向我展示了 3 种方法:

requestLocationUpdates(LocationRequest request, LocationListener listener)
requestLocationUpdates(LocationRequest request, PendingIntent CallbackIntent)
requestLocationUpdates(LocationRequest request, LocationListener listener, Looper looper)

所以我认为第一个在这个地方是最正确的。我应该在LocationListener 插槽中放置什么?我寻求帮助,但几乎没有解释它是如何工作的。

【问题讨论】:

    标签: android location updates locationlistener


    【解决方案1】:

    您正在活动 MainActivity 中实现 LocationListener。因此,并发位置更新的调用将是这样的:

    mLocationClient.requestLocationUpdates(mLocationRequest, this);
    

    确保您正在实施的 LocationListener 来自 google api,即导入:

    import com.google.android.gms.location.LocationListener;
    

    不是这个:

    import android.location.LocationListener;
    

    它应该可以正常工作。

    在您执行此操作之前,LocationClient 确实已连接也很重要。我建议你不要在 onCreate 或 onStart 方法中调用它,而是在 onResume 中调用它。 Google Location Api 的教程中对这一切都进行了很好的解释:https://developer.android.com/training/location/index.html

    【讨论】:

    • 正是我搞砸的。谢谢你。 :)
    • 所以我知道 requestLocationUpdates 会将 Location 返回到 PendingIntent 但我在哪里可以访问返回的位置?
    • 要调用此请求,您需要一个类来实现 LocationUpdates。此实现具有方法 OnLocationChanged(Location location)。这是您访问返回位置的地方。公共类 MyLocationListener 实现 LocationListener {...}
    【解决方案2】:

    我用这个:

    LocationManager.requestLocationUpdates(String provider, long minTime, float minDistance, LocationListener listener)
    

    例如,使用 1s 的间隔:

    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000,0,this);
    

    时间以毫秒为单位,距离以米为单位。

    这会自动调用:

    public void onLocationChanged(Location location) {
        //Code here, location.getAccuracy(), location.getLongitude() etc...
    }
    

    我也将这些包含在脚本中,但实际上并没有使用它们:

    public void onStatusChanged(String provider, int status, Bundle extras) {}
    public void onProviderEnabled(String provider) {}
    public void onProviderDisabled(String provider) {}
    

    简而言之:

    public class GPSClass implements LocationListener {
    
        public void onLocationChanged(Location location) {
            // Called when a new location is found by the network location provider.
            Log.i("Message: ","Location changed, " + location.getAccuracy() + " , " + location.getLatitude()+ "," + location.getLongitude());
        }
    
        public void onStatusChanged(String provider, int status, Bundle extras) {}
        public void onProviderEnabled(String provider) {}
        public void onProviderDisabled(String provider) {}
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000,0,this);
        }
    }
    

    【讨论】:

    • 距离总是米。
    • 它不会自动运行。当位置改变时调用 onLocationChanged。在某些情况下,这需要时间
    • @JimmyKane 我将最小距离设置为 0,更新间隔按预期工作。当最小距离较高时,如果未超过该距离阈值,则不会触发位置更新。我是这么理解的。
    猜你喜欢
    • 2011-11-20
    • 1970-01-01
    • 2019-12-09
    • 1970-01-01
    • 2019-07-11
    • 2021-09-26
    • 2011-03-18
    • 2015-06-16
    • 2012-08-31
    相关资源
    最近更新 更多