它是如何工作 - requestLocationUpdates()+ LocationRequest /监听器 [英] How does it work - requestLocationUpdates() + LocationRequest/Listener

查看:447
本文介绍了它是如何工作 - requestLocationUpdates()+ LocationRequest /监听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新的Andr​​oid coder和我有问题,要求对我的定位更新。

I am new Android coder and I have problem with requesting updates for my localization.

我的工作从<一个教程href="http://developer.android.com/training/location/receive-location-updates.html">http://developer.android.com/training/location/receive-location-updates.html

我的应用程序能够处理异常,获得正确的纬度和经度,和地理codeR可以处理显示的联系地址。但我要问的位置只有一次 - 或当位置的变化。我想这样做的时间间隔。现在我开始从教程执行code,它看起来像:

My application can handle exceptions, getting latitude and longitute properly, and geocoder can handle displaying the adress. But I ask for location only once - or when location changes. I would like to do time intervals. For now I started implementing code from the tutorials and it looks like that:

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种方法:

So there are already intervals implemented and location request. But in the link I gave before there is a comment that I should use somewhere requestLocationUpdates() (probably onCreate(), onStart() and removal of request on onStop()), but I have problem with it. So, Eclipse shows me 3 methods:

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

所以第一个我认为最正确的在这个地方。我应该在 LocationListener的槽的地方?我寻求帮助,很少解释它是如何工作的。

So the first one I think is most right in this place. What should I place in LocationListener slot? I ask for help with little explanation how it works.

推荐答案

您在您的活动MainActivity实施LocationListener的。呼叫并发位置更新将为此是这样的:

You are implementing LocationListener in your activity MainActivity. The call for concurrent location updates will therefor be like this:

mLocationClient.requestLocationUpdates(mLocationRequest, this);

请确保你实施LocationListener的是谷歌的API,那就是导入这样的:

Be sure that the LocationListener you're implementing is from the google api, that is import this:

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

而不是这样的:

and not this:

import android.location.LocationListener;

和它应该工作得很好。

这也是非常重要的LocationClient真的这样做之前连接。我建议你​​不要把它在OnCreate或ONSTART方法,但在onResume。这是所有的解释相当不错的教程谷歌定位API:<一href="https://developer.android.com/training/location/index.html">https://developer.android.com/training/location/index.html

It's also important that the LocationClient really is connected before you do this. I suggest you don't call it in the onCreate or onStart methods, but in onResume. It is all explained quite well in the tutorial for Google Location Api: https://developer.android.com/training/location/index.html

这篇关于它是如何工作 - requestLocationUpdates()+ LocationRequest /监听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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