【问题标题】:Android Location Update ClarificationAndroid 位置更新说明
【发布时间】:2015-01-17 23:12:34
【问题描述】:

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

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

关于位置更新我有以下澄清

1) 我使用 Pending Intent 请求位置。我给了时间 位置请求中的间隔为 5 分钟。我正在获取位置 每 5 分钟成功获取信息。但我的问题是“如何有效 Android 使用 GPS 获取位置 - GPS 无需开启 整个 5 分钟”。大约 4 分 50 秒,它开始使用 GPS 获取用户位置。我只想知道融合的位置提供者如何有效地使用 GPS。

2) 我还想知道获取用户位置所花费的时间 通过使用可用的提供程序。大约需要多少时间 android api通过使用融合位置获取用户位置 提供者。

3) Fused Location Provider 仅在给定间隔的最近时间使用 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 位置更新的开发人员。

提前致谢。

【问题讨论】:

    标签: android gps location


    【解决方案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);
    
                                                        }
                     }
              }
           };
    

    【讨论】:

    • 我们分析并最终想到使用Android SDK提供的位置管理器来获取位置信息。即使您使用 Google Play 服务来获取位置信息,缺点是您无法支持大多数最终用户,这是因为大多数设备都没有安装 Google Play 商店。另一个是与位置 api 相比,准确性并没有那么好。
    • 嗨 Vignesh,是的,设备上应该安装了 Google PlayStore。但我不确定使用 Fused Location Provider 是否不如使用 Location manager API 准确。您如何确认这一点?
    • 使用fusedLocation Provider api,您可以配置为高精度或低功耗。它比 Location Manager api 提供更准确的结果。
    • 不,我已经绘制了两个结果。位置管理器点被绘制在道路路径中。但 Fused Api 点距离道路路径稍远。
    • 如果您使用的是位置管理器,那么您必须定义自己的逻辑来影响电池。我们已经定义了一个逻辑,使得该逻辑消耗的功率更少。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-26
    • 2019-10-26
    相关资源
    最近更新 更多