Android位置更新澄清

时间:2014-11-20 07:06:39

标签: android gps location

正在使用位置更新应用程序,它将定期更新用户位置到服务器。

我使用了融合位置提供程序来获取位置更新。我引用了以下链接Android Location Update

关于位置更新,我有以下 说明

  

1)我使用Pending Intent请求了位置。我给了时间   间隔为5分钟的位置请求。我到了这个位置   信息每5分钟成功一次。但我的问题是"效率如何   Android使用GPS获取位置 - GPS无需启用   整整5分钟#34;它开始使用GPS到大约4分50秒   获取用户位置。我只是想知道融合位置提供商如何有效地使用GPS。

     

2)而且我想知道获取用户位置所花费的时间   通过使用可用提供商。大概花了多少时间   android api通过使用融合位置获取用户位置   供应商。

     

3)融合位置提供商仅在给定间隔的最近时间使用GPS   用户位置。 GPS无法使用的剩余时间。在那儿   是更好的解决方案关闭GP或有效的使用方式   GPS可以节省移动电池电量。

     

4)我给出的时间间隔为1分钟。有时候我没有得到   位置每隔一分钟更新一次。例如,第一分钟成功   我正在获取用户位置。然后在第二分钟没有获得用户位置。然后第3分钟   越来越好了。我有GPS开启和移动网络可用和WiFi   连接的。

     

5)我们可以给出的最大时间间隔是多少。我找不到   文件中的任何最长期限。我们可以将最小时间间隔设为零。但不建议这样做。

     

6)我还想知道使用此功能时的最低操作系统支持。我在下面提到了链接Does Google Activity Recognition work on older versions of Android?,其中说Google Play服务中的所有内容都应该恢复到API级别8(Android 2.3)。

请帮我解决这个问题。我希望这可以帮助其他正在开发Android位置更新的开发人员。

提前致谢。

1 个答案:

答案 0 :(得分:1)

I have used the fused location provider API's in our project and it's was really helpful to improve battery usage.Earlier We were using Android's Location framework APi's .

Please read below article which I have prepared ,you can refer the information to implement it in your project.

In simple words, it’s the best way to get location data in Android platform as of now.
GooglePlay Services provide many location APi’s to get the location data(e.g. User’s current location or you can say device’s last known location).

The Fused Location Provider is one of the location APIs in Google Play services.
Prerequisite is that:
1-        Google Play services sdk is used as library project(and also Google PlayService is properly installed in your device)
Download and install the Google Play services component from  the SDK Manager and add the library to your project.
Import GooglePlayServices lib from android google-play-services_lib in  your development  project as  Library project.
2-      You should have an actual device as this APi won’t work in Emulator.

The Fused Location Provider intelligently manages the underlying location technology (GPS/Wi-Fi/Network provider’s connection) and gives us the best location according to our needs.

Why to use
============= 
We could choose one of the location providers (network or GPS) and request location updates or set up proximity alert. But there were two main issues with this approach:
1. In case we need to define precise location, we had to switch between network and GPS location providers (as GPS doesn’t work indoors).
2. Proximity alerts were used to notify a user about proximity to a location, and this took its toll on the battery life.

Benefits
 ==========
1. Simple APIs: Lets us specify high-level needs like “high accuracy” or “low power”, instead of having to worry about location providers.
2. Battery efficient: Minimizes out app’s use of power. Based on all incoming location requests and available sensors, fused location provider chooses the most efficient way to meet those needs.

Steps to use this Api:
 =====================
1-      Declare the location related permission in the manifest file.

<uses-permission android:name="android.permission.ACCESS_COURSE_LOCATION"/>
  Or
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>


2. Implement related interfaces and callbacks.

  Before we request location updates, we must first implement the interfaces that Location Services uses to communicate connection status to our app:
2.1 com.google.android.gms.common.GooglePlayServicesClient.ConnectionCallbacks: Specifies methods that Location Services calls when a location client is connected or disconnected.
2.2
com.google.android.gms.common.GooglePlayServicesClient.OnConnectionFailedListener
3 Connect to Google Play Services

Connecting LocationClient to Google api.To do this , create a LocationClient object (it’s actually instance of GoogleApiClient object)  as below:

mLocationClient = new GoogleApiClient.Builder(getApplicationContext())
                                                 .addApi(LocationServices.API).addConnectionCallbacks(this)
                                                 .addOnConnectionFailedListener(this).build();

and then call connect() :
mLocationClient.connect();

4- Create an instance of FusedLocationProviderApi by using LocationServices class as below:

private FusedLocationProviderApi fusedLocationProviderApi = LocationServices.FusedLocationApi;

5- Retrieve the current location

 Inside onConnected(Bundle bundle){

Location currentLocation = fusedLocationProviderApi                                  .getLastLocation(mLocationClient);
                 if (mCurrentLocation != null) {
                                 Log.d(TAG, "last location =" +           mCurrentLocation.getLatitude()
                                                                 + " - " + mCurrentLocation.getLongitude());
 }              
 }

6-  Receive periodic location updates

 Create and setup LocationRequest object:
mLocationRequest = new LocationRequest();

private void setLocationParameter() {
                 // Set the update interval
                 mLocationRequest.setInterval(Constants.SECONDS_TO_UP);
                 // Use high accuracy
                 mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
                 // Set the interval ceiling to one minute
                 mLocationRequest.setFastestInterval(Constants.SECONDS_TO_UP);
                 // Set the distance to 10 meters.
                 mLocationRequest.setSmallestDisplacement(Constants.METERS_TO_UP);
   }


6- Request for periodic Location updates: To get periodic location updates from Location Services, we send a request using a location client.

LocationListener listener =  new LocationListener() {
           @Override
   public void onLocationChanged(Location location) {    
                                 Utils.locationUpdates = Utils.locationUpdates + 1;
                                 if (Utils.locationUpdates == 1) {
                                                 mLocationRequest
                                                                                 .setPriority(LocationRequest.PRIORITY_LOW_POWER);
LocationServices.FusedLocationApi.requestLocationUpdates(
                                                 mLocationClient, mLocationRequest, listener);

                                                    }
                 }
          }
       };