简体   繁体   English

它是如何工作的-requestLocationUpdates()+ LocationRequest / Listener

[英]How does it work - requestLocationUpdates() + LocationRequest/Listener

I am new Android coder and I have problem with requesting updates for my localization. 我是新的Android编码器,在为本地化请求更新时遇到问题。

I working with tutorials from http://developer.android.com/training/location/receive-location-updates.html . 我正在使用http://developer.android.com/training/location/receive-location-updates.html中的教程。

My application can handle exceptions, getting latitude and longitute properly, and geocoder can handle displaying the adress. 我的应用程序可以处理异常,适当地获得纬度和经度,而Geocoder可以处理显示地址。 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();
}

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. 但是在我之前给出的链接中有一条评论,我应该在某处使用requestLocationUpdates() (可能是onCreate()onStart()并删除onStop()上的请求),但是我对此有onStop() So, Eclipse shows me 3 methods: 因此,Eclipse向我展示了3种方法:

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

So the first one I think is most right in this place. 因此,我认为第一个在这个地方最合适。 What should I place in LocationListener slot? 我应该在LocationListener插槽中放置什么? I ask for help with little explanation how it works. 我寻求帮助时几乎没有解释它是如何工作的。

You are implementing LocationListener in your activity MainActivity. 您正在活动MainActivity中实现LocationListener。 The call for concurrent location updates will therefor be like this: 因此,并发位置更新的调用将如下所示:

mLocationClient.requestLocationUpdates(mLocationRequest, this);

Be sure that the LocationListener you're implementing is from the google api, that is import this: 确保您要实现的LocationListener来自google api,即导入以下内容:

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

and not this: 而不是这样:

import android.location.LocationListener;

and it should work just fine. 它应该可以正常工作。

It's also important that the LocationClient really is connected before you do this. 在执行此操作之前,必须真正连接LocationClient,这一点也很重要。 I suggest you don't call it in the onCreate or onStart methods, but in onResume. 我建议您不要在onCreate或onStart方法中调用它,而应在onResume中调用它。 It is all explained quite well in the tutorial for Google Location Api: https://developer.android.com/training/location/index.html 在Google Location Api的教程中对此进行了很好的解释: https//developer.android.com/training/location/index.html

I use this one: 我用这个:

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

For example, using a 1s interval: 例如,使用1s间隔:

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

the time is in milliseconds, the distance is in meters. 时间以毫秒为单位,距离以米为单位。

This automatically calls: 这会自动调用:

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

I also had these included in the script but didnt actually use them: 我也将这些包含在脚本中,但实际上并未使用它们:

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

In short: 简而言之:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 LocationRequest 和 LocationClient.requestLocationUpdates - LocationRequest and LocationClient.requestLocationUpdates 如何检查我们是否已经调用fusedLocationProviderClient.requestLocationUpdates(locationRequest,endingIndent); - how to check if we already call fusedLocationProviderClient.requestLocationUpdates(locationRequest, pendingIndent); 如何在Android中实现LocationManager的requestLocationUpdates? - How does really work the requestLocationUpdates of LocationManager in Android? FusedLocationProviderApi:LocationRequest.setPriority如何与“位置模式”设备设置一起使用? - FusedLocationProviderApi: how does LocationRequest.setPriority work with “Location mode” device setting? requestLocationUpdates vs Location Listener - requestLocationUpdates vs Location Listener addProximityAlert不起作用(requestLocationUpdates也不起作用) - Android - addProximityAlert doesn't work (neither does requestLocationUpdates) - Android requestLocationUpdates根本不起作用 - requestLocationUpdates do not work at all 获取 GPS 的 requestLocationUpdates 不起作用 - The requestLocationUpdates to get GPS not work 即使参数0,requestLocationUpdates()如何获取位置? - How does requestLocationUpdates() get location even though has paramter 0? LocationManager requestLocationUpdates不起作用 - LocationManager requestLocationUpdates doesn't work
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM