如何获得位置时,它改变 [英] How to get location when it changes
问题描述
我想知道,有一种方式来获得的位置,如果只有它的变化?我知道Android提供此 http://developer.android.com/training/位置/接收位置updates.html 获得位置更新,因为它规定:
I would like to know there is a way to get location if only it changes? I know android provides this http://developer.android.com/training/location/receive-location-updates.html to get location updates and as it states:
如果你的应用程序做导航或跟踪,你可能想获取用户的位置,定期进行。虽然可以用LocationClient.getLastLocation()做到这一点,更直接的办法是要求从位置服务定期更新。在对此,定位服务自动更新提供当前最佳的位置您的应用程序的基础上,当前可用的位置,供应商,如WiFi和GPS。
"If your app does navigation or tracking, you probably want to get the user's location at regular intervals. While you can do this with LocationClient.getLastLocation(), a more direct approach is to request periodic updates from Location Services. In response, Location Services automatically updates your app with the best available location, based on the currently-available location providers such as WiFi and GPS."
怎么做,而不使用固定时间间隔?其主要目标是最大限度地降低功耗。例如,如果我在一个房间里坐着,不会有位置的变化。如果我开始移动,它应该报告的位置变化。
How to do it without using regular intervals? The main goal is to minimize power consumption. For example, if i am in a room and sitting, there will be no location change. If i start moving, it should report the location changes.
我确实需要得到,如果只更新位置,所以不会经常。
I do need to get locations if only update, so not constantly.
的方法,应在后台持续运行,并提供以功率有效的方式全部位置更新。
The method should run on the background continuously and provide the all location updates in a power efficient way.
我在这里找到Android的融合定位API https://developer.android.com /google/play-services/location.html
I found android fused location API here https://developer.android.com/google/play-services/location.html
有关使用该如何?
我可以实现它不使用定期?
Can i implement it without using regular intervals?
推荐答案
此方法需要GooglePlayService,它并不需要多大的力量,也许你可以试试。
This method requires GooglePlayService, and it doesn't need much power, maybe you can try it.
private LocationClient locationClient;
private static final int LOCATION_UPDATES_INTERVAL = 10000;
private static final int LOCATION_NUM_UPDATES = 5;
public void startLocationUpdate() {
if (!(locationClient.isConnected() || locationClient.isConnecting())) {
locationClient.connect(); // Somehow it becomes connected here
return;
}
// Request for location updates
LocationRequest request = LocationRequest.create();
request.setInterval(LOCATION_UPDATES_INTERVAL);
request.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
request.setNumUpdates(LOCATION_NUM_UPDATES);
locationClient.requestLocationUpdates(request, this);
}
而在活动中,您应该实现以下回调
And in the Activity you should implement the following callback
private Location mLastLocation;
@Override
public void onLocationChanged(Location location) {
if (location == null) {
Log.v(TAG, "onLocationChanged: location == null");
return;
}
if (mLastLocation != null &&
mLastLocation.getLatitude() == location.getLatitude() &&
mLastLocation.getLongitude() == location.getLongitude()) {
return;
}
LogUtil.i(TAG, "LocationChanged == @" +
location.getLatitude() + "," + location.getLongitude());
mLastLocation = location;
}
这篇关于如何获得位置时,它改变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!