【问题标题】:Android's most accurate Location APIAndroid 最准确的 Location API
【发布时间】:2016-11-29 10:25:40
【问题描述】:

为了工作,我的应用需要一个位置 API。 我打算使用 Mapbox 平台来定制它的设计(因为谷歌地图 就我而言,不提供这种级别的自定义)。

文档说我们应该在构建位置时使用 Google Play API 应用:

Google Play 服务位置 API 优于 Android 框架位置 API (android.location) 作为一种添加方式 位置感知到您的应用程序。如果您当前使用的是安卓 框架位置 API,强烈建议您切换到 尽快提供 Google Play 服务位置 API。

我的问题是: 在 GPS 精度方面,Google Play API 是不是最高效的 API? 或者我应该使用 LocationManager 和 LocationListener 的方式吗?

我需要准确性。我应该使用哪一个? 谢谢。

【问题讨论】:

  • 所以如果它是准确的但会耗尽电池,你可以接受吗?
  • 一开始,是的!这就是他们推荐 Google Play API 的原因吗?
  • 我认为fusedlocation(又名google location api)旨在提高电池使用和网络的效率。我还没有看到任何基准,所以这都是主观和推测的;我从fusedlocation 获得的准确度绰绰有余,除非它表现得非常糟糕,否则我不会尝试任何东西。
  • @muratgu 精确到多少米?
  • 融合定位使用网络和 GPS 来提供比 GPS 更低的准确度,但耗电量更少。它的准确性取决于很多因素。 GPS alon 精确到 5-10m 左右,但取决于手机硬件、清晰的视线、大气条件等。融合定位会不太准确,并且有许多相同的条件。网络将是最少的,在 200m 以内,但几乎不使用电力。 Fused 和 Network 都需要网络连接(GPS 不需要)。

标签: android location


【解决方案1】:

在 android 中也有三种类型的位置:

  1. GPS_PROVIDER
  2. NETWORK_PROVIDER
  3. PASSIVE_PROVIDER

所以,根据我的编码经验,我知道如果你使用:

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 10, new MyLocationListener());

您将获得高达 14+ 小数位的精度。

但是如果你像这样使用它们的融合:

LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, my_google_listener);

您将获得高达 6 到 7 位小数的精度。试试吧 !!! reference

但请注意,GPS Provider 需要时间来获取位置,而 Google Location 则要快得多,因为它从 API 调用获取数据到其 google 服务器数据库。

GPS 离线工作,而 google 提供商通过手机或 wifi 数据获取位置。

【讨论】:

  • 但是 - 小数位数是否与实际位置精度相关?我的猜测是不会。
  • ^ 是的!小数位数会影响位置的准确性。更精确的坐标意味着更准确的位置。
  • 没有更精确的坐标并不意味着更精确,它只是意味着更多的小数位。小数位数应根据准确性进行调整,但我在这里看不到任何事实表明如此。
【解决方案2】:

使用FusedLocationProviderApi并将LocationRequest优先级设置为PRIORITY_HIGH_ACCURACY

这是用于准确获取位置的最新 API,Google 建议使用相同的 API。

检查准确性详细信息here

基本上,Google play services API 具有通过融合 GPS+NetworkProvider+被动提供商获得准确位置的智能。

【讨论】:

    【解决方案3】:

    FusedLocationProviderApi 已弃用。您应该使用FusedLocationProviderClient 并且应该添加ACCESS_FINE_LOCATION 权限,而不是ACCESS_COARSE_LOCATION 以获得最准确的位置。

    阅读this 文章,了解为什么FusedLocationProviderClient 是最佳解决方案。

    【讨论】:

      【解决方案4】:

      根据我的经验,Google Services Location API 更适合用于以下方面:

      • 他们处理用户设置,选择最佳的可用位置提供商。如果您选择直接使用 LocationManager,则需要您的代码来处理。
      • 您可能希望以更少的电量获得更好的位置,因为 Google 会定期更新其使用 WiFi、手机信号塔等确定位置的算法。

      根据我的经验,在使用 Google 服务时,在许多情况下,对于地图应用程序(即几十米)而言,具有足够精确度的位置并不需要 GPS 数据。不过,FusedLocationProvider 也可能是这种情况,但电池使用量可能是个例外。

      总而言之,如果您没有理由不使用 Google 服务(例如 - 您定位的国家/地区不提供这些服务,或者希望通过其他市场进行分发),您应该使用他们的 Location API。

      【讨论】:

        【解决方案5】:

        您应该使用 LocationManager 来确保准确性。 你也可以使用这个类。

        //GPSTracker.java
        import android.app.AlertDialog;
        import android.app.Service;
        import android.content.Context;
        import android.content.DialogInterface;
        import android.content.Intent;
        import android.location.Location;
        import android.location.LocationListener;
        import android.location.LocationManager;
        import android.os.Bundle;
        import android.os.IBinder;
        import android.provider.Settings;
        import android.util.Log;
        
        public class GPSTracker extends Service implements LocationListener {
        
        private final Context mContext;
        
        // flag for GPS status
        boolean isGPSEnabled = false;
        
        // flag for network status
        boolean isNetworkEnabled = false;
        
        // flag for GPS status
        boolean canGetLocation = false;
        
        Location location; // location
        double latitude; // latitude
        double longitude; // longitude
        float bearing; // bearing
        
        // The minimum distance to change Updates in meters
        private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters
        
        // The minimum time between updates in milliseconds
        private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute
        
        // Declaring a Location Manager
        protected LocationManager locationManager;
        
        public GPSTracker(Context context) {
            this.mContext = context;
            getLocation();
        }
        
        public Location getLocation() {
            try {
                locationManager = (LocationManager) mContext
                        .getSystemService(LOCATION_SERVICE);
        
                // getting GPS status
                isGPSEnabled = locationManager
                        .isProviderEnabled(LocationManager.GPS_PROVIDER);
        
                // getting network status
                isNetworkEnabled = locationManager
                        .isProviderEnabled(LocationManager.NETWORK_PROVIDER);
        
                if (!isGPSEnabled && !isNetworkEnabled) {
                    // no network provider is enabled
                } else {
                    this.canGetLocation = true;
                    if (isNetworkEnabled) {
                        locationManager.requestLocationUpdates(
                                LocationManager.NETWORK_PROVIDER,
                                MIN_TIME_BW_UPDATES,
                                MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                        Log.d("Network", "Network");
                        if (locationManager != null) {
                            location = locationManager
                                    .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                            if (location != null) {
                                latitude = location.getLatitude();
                                longitude = location.getLongitude();
                                bearing = location.getBearing();
                            }
                        }
                    }
                    // if GPS Enabled get lat/long using GPS Services
                    if (isGPSEnabled) {
                        if (location == null) {
                            locationManager.requestLocationUpdates(
                                    LocationManager.GPS_PROVIDER,
                                    MIN_TIME_BW_UPDATES,
                                    MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                            Log.d("GPS Enabled", "GPS Enabled");
                            if (locationManager != null) {
                                location = locationManager
                                        .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                                if (location != null) {
                                    latitude = location.getLatitude();
                                    longitude = location.getLongitude();
                                    bearing = location.getBearing();
                                }
                            }
                        }
                    }
                }
        
            } catch (Exception e) {
                e.printStackTrace();
            }
        
            return location;
        }
        
        /**
         * Stop using GPS listener
         * Calling this function will stop using GPS in your app
         * */
        public void stopUsingGPS(){
            if(locationManager != null){
                locationManager.removeUpdates(GPSTracker.this);
            }
        }
        
        /**
         * Function to get latitude
         * */
        public double getLatitude(){
            if(location != null){
                latitude = location.getLatitude();
            }
        
            // return latitude
            return latitude;
        }
        
        /**
         * Function to get longitude
         * */
        public double getLongitude(){
            if(location != null){
                longitude = location.getLongitude();
            }
        
            // return longitude
            return longitude;
        }
        
        public float getBearing() {
            if (location != null) {
                bearing = location.getBearing();
            }
            return bearing;
        }
        
        /**
         * Function to check GPS/wifi enabled
         * @return boolean
         * */
        public boolean canGetLocation() {
            return this.canGetLocation;
        }
        
        /**
         * Function to show settings alert dialog
         * On pressing Settings button will lauch Settings Options
         * */
        public void showSettingsAlert(){
            AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);
        
            // Setting Dialog Title
            alertDialog.setTitle("GPS is settings");
        
            // Setting Dialog Message
            alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");
        
            // On pressing Settings button
            alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog,int which) {
                    Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                    mContext.startActivity(intent);
                }
            });
        
            // on pressing cancel button
            alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });
        
            // Showing Alert Message
            alertDialog.show();
        }
        
        @Override
        public void onLocationChanged(Location location) {
        }
        
        @Override
        public void onProviderDisabled(String provider) {
        }
        
        @Override
        public void onProviderEnabled(String provider) {
        }
        
        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
        }
        
        @Override
        public IBinder onBind(Intent arg0) {
            return null;
        }
        
        }
        
        //MainActivity.java
        GPSTracker gps = new GPSTracker(MainActivity.this);
        

        【讨论】:

          猜你喜欢
          • 2016-12-09
          • 2016-04-11
          • 1970-01-01
          • 2015-04-06
          • 2013-05-27
          • 2014-11-05
          • 2016-07-31
          • 1970-01-01
          • 2018-06-02
          相关资源
          最近更新 更多