【问题标题】:How to implement android documentation code for comparing multiple location results?如何实现用于比较多个位置结果的android文档代码?
【发布时间】:2012-12-31 20:50:48
【问题描述】:

我正在开发一个位置感知应用程序,当我请求位置更新时,我偶尔会收到一个非常旧的位置(好像它没有更新),或者我收到多个位置通知。我开始深入研究这个问题,发现this blog 描述了 ANDROIDS 位置监听器的工作原理。

简而言之,我的解释是,当您requestLocationUpdates 时,您不会只获得一个位置对象,而是会收到多个。于是我开始试图弄清楚如何从多个位置对象中挑选出最佳位置对象,并在 Android documentation 中找到了一个算法(在“维护当前最佳估计”部分下)

我对如何将该部分中的代码块实现到我自己的应用程序中感到困惑。代码块接受两个参数locationcurrentbestlocation 并比较它们。

  1. 如何声明两个位置对象以供代码块进行比较? (代码示例或伪代码将不胜感激!)
  2. 我对@9​​87654326@ 提供多个位置对象的理解是否正确?
  3. 如何将 GOOGLES 代码应用到我的代码中?见下文:

我的代码如下:

public class MainActivity extends Activity {
    LocationManager lm;
    LocationListener ll;
    private Location previousLocation;

    public void onCreate(Context context, Intent intent) {    
        lm = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
        ll = new myListener();
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 6000, 1000, ll);
    }

    private class myListener implements LocationListener {      
        public void onLocationChanged(Location loc) {
        if (previousLocation == null) {
               previousLocation = loc;
            } else {
                if (isBetterLocation(loc, previousLocation)) {
                //NOTIFICATION NEW LOCATION IS BETTER
                } else {
                //NOTIFICATION PREVIOUS LOCATION IS BETTER
                }
            }
        }
        public void onProviderDisabled(String provider) {
        }
        public void onProviderEnabled(String provider) {
        }
        public void onStatusChanged(String provider, int status, Bundle extras) {
        }
    }

    //GOOGLE ANDROID DOCUMENTATION CODE FOR MAINTAINING CURRENT BEST ESTIMATE
    private static final int TWO_MINUTES = 1000 * 60 * 2;

    protected boolean isBetterLocation(Location location, Location currentBestLocation) {
        if (currentBestLocation == null) {
            // A new location is always better than no location
            return true;
        }

        // Check whether the new location fix is newer or older
        long timeDelta = location.getTime() - currentBestLocation.getTime();
        boolean isSignificantlyNewer = timeDelta > TWO_MINUTES;
        boolean isSignificantlyOlder = timeDelta < -TWO_MINUTES;
        boolean isNewer = timeDelta > 0;

        // If it's been more than two minutes since the current location, use the new location
        // because the user has likely moved
        if (isSignificantlyNewer) {
            return true;
        // If the new location is more than two minutes older, it must be worse
        } else if (isSignificantlyOlder) {
            return false;
        }

        // Check whether the new location fix is more or less accurate
        int accuracyDelta = (int) (location.getAccuracy() - currentBestLocation.getAccuracy());
        boolean isLessAccurate = accuracyDelta > 0;
        boolean isMoreAccurate = accuracyDelta < 0;
        boolean isSignificantlyLessAccurate = accuracyDelta > 200;

        // Check if the old and new location are from the same provider
        boolean isFromSameProvider = isSameProvider(location.getProvider(), currentBestLocation.getProvider());

        // Determine location quality using a combination of timeliness and accuracy
        if (isMoreAccurate) {
            return true;
        } else if (isNewer && !isLessAccurate) {
            return true;
        } else if (isNewer && !isSignificantlyLessAccurate && isFromSameProvider) {
            return true;
        }
        return false;
    }

    private boolean isSameProvider(String provider1, String provider2) {
        if (provider1 == null) {
            return provider2 == null;
        }
            return provider1.equals(provider2);
    }
}

【问题讨论】:

    标签: android geolocation location locationlistener


    【解决方案1】:

    当您使用LocationManager 请求位置更新时,它会继续为您提供更新,直到您告诉它停止或您的应用程序被终止。这样,您可以每隔一段时间监控设备的当前位置,以便知道它们何时移动。如果您只需要一次位置更新,则在收到足够准确的位置修复后,请致电locationManager.removeUpdates(listener)

    如果您只打算支持运行 Gingerbread 及更高版本的设备,您也可以使用requestSingleUpdate(java.lang.String, android.location.LocationListener, android.os.Looper) 方法。

    首先要做的是声明您的位置侦听器并注册更新。这段代码应该让你开始:

    public class MyActivity extends Activity implements LocationListener {
        /** Cache the last location fix received. */
        private Location mLastLocationReceived;
    
        @Override
        public void onResume() {
            super.onResume();
    
            // Register our location listener
            LocationManager lm = (LocationManager) getSystemService(LOCATION_SERVICE);
            lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 10000, 500, this);
        }
    
        @Override
        public void onPause() {
            super.onPause();
    
            // Unregister our location listener
            LocationManager lm = (LocationManager) getSystemService(LOCATION_SERVICE);
            lm.removeUpdates(this);
        }
    
        @Override
        public void onLocationChanged(Location location) {
            if (mLastLocationReceived == null) {
                mLastLocationReceived = location;
            } else {
                if (isBetterLocation(location, mLastLocationReceived)) {
                    // New location fix is better!
                } else {
                    // New location fix is not better!
                }
            }
        }
    
        @Override
        public void onProviderDisabled(String provider) {
            // Pass
        }
    
        @Override
        public void onProviderEnabled(String provider) {
            // Pass
        }
    
        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            // Pass
        }
    
        private boolean isBetterLocation(Location newLocation,
                Location oldLocation) {
    
            // TODO: Implement the logic to determine if the new location is
            // of better quality than the old location. Your application's
            // business logic determines what this method should do.
    
            return false;
        }
    
    }
    

    请注意,isBetterLocation 方法始终返回 false,并由您提供实现。有一个很好的示例实现here。另外,我没有测试此代码,所以请原谅任何错误。

    【讨论】:

    • 抱歉 android/java 新手。 mLastLocationReceived 是一个特殊变量,还是我必须将 mLastLocationReceived 定义为收到的最后一个位置?
    • 这是一个你定义的变量。它可以被称为任何你喜欢的。它只是一个member variable 范围为您的类实例。
    • 如何将其定义为先前的位置对象?我在上面添加了我的代码......对不起所有的菜鸟问题。
    • 我不确定你的意思。 onLocationChanged 方法检查是否已设置 mLastLocationReceived。如果有,它将它与新的位置对象进行比较,如果尚未设置,则将当前位置对象分配给它,以便在下一个位置修复出现时进行比较。
    • 我没有收到任何关于先前位置或新位置是否更好的通知...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-13
    相关资源
    最近更新 更多