Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了location和currentBestLocation来自哪里?在Android开发指南(获取用户位置)大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我在 Android Dev Guid中阅读了关于 Obtaining User Location的教程,
我尝试将其改编为以下代码..但我不知道我应该将哪个位置值放入isBetterLOCATIOn(位置位置,位置currentBestLOCATIOn)

Example.class

private LOCATIOnManager lOCMan;

        @Override
        protected void onCreate(Bundle savedInstanceStatE) {
        super.onCreate(savedInstanceStatE);
        setContentView(R.layout.main);

            String context = Context.LOCATION_serviCE;
            lOCMan = (LOCATIOnManager)getSystemservice(context);

            Criteria criteria = new Criteria();
            criteria.setAccuracy(Criteria.ACCURACY_FINE);
            criteria.setAltituderequired(false);
            criteria.setBearingrequired(false);
            criteria.setPowerrequirement(Criteria.POWER_LOW);

            String provider = lOCMan.getBestProvider(criteria,truE);
            lOCMan.requestLOCATIOnupdates(
                    provider,MIN_TIME,MIN_DISTANCE,LOCATIOnListener);

        }

        private LOCATIOnListener LOCATIOnListener = new LOCATIOnListener(){

        @Override
        public void onLOCATIOnChanged(LOCATIOn LOCATIOn) {
            // what should i pass as first and second parameter in this method
            if(isBetterLOCATIOn(LOCATIOn1,LOCATIOn2)){
               // isBetterLOCATIOn = true > do updateLOCATIOn 
               updateLOCATIOn(LOCATIOn);
            }
        }

        @Override
        public void onProviderDisabled(String provider) {}
        @Override
        public void onProviderEnabled(String provider) {}
        @Override
        public void onStatusChanged(String provider,int status,Bundle extras) {}

       };

     protected Boolean isBetterLOCATIOn(LOCATIOn LOCATIOn,LOCATIOn currentBestLOCATIOn) {
         if (currentBestLOCATIOn == null) {
            // A new LOCATIOn is always better than no LOCATIOn
            return true;
         }
          //Brief ... See code in Android Dev Guid "Obtaining User LOCATIOn"
     }

解决方法

真的不是那么难.代码的作用是接收到找到的位置的连续更新;您可以让多个听众收听不同的提供商,因此这些更新可能或多或少准确,具体取决于提供商(例如GPS可能比网络更准确). isBetterLOCATIOn(…)评估侦听器找到的位置是否实际上比您已经知道的位置更好(并且应该在您的代码中引用). isBetterLOCATIOn(…)代码已有详细记录,因此不难理解,但第一个参数位置是提供程序找到的新位置,而currentBestLOCATIOn是您已经知道的位置.

我使用的代码与您的代码大致相同,除了我不仅仅是最好的提供者.
处理程序的东西是因为我不想继续更新,只需在两分钟的最大时间内找到足够准确的最佳位置(GPS可能需要一点).

private LOCATIOn currentBestLOCATIOn = null;
private serviceLOCATIOnListener gpsLOCATIOnListener;
private serviceLOCATIOnListener networkLOCATIOnListener;
private serviceLOCATIOnListener passiveLOCATIOnListener;
private LOCATIOnManager LOCATIOnManager;

private Handler handler = new Handler();


public void fetchLOCATIOn() {
    LOCATIOnManager = (LOCATIOnManager) this.getSystemservice(Context.LOCATION_serviCE);

    try {
        LOCATIOnProvider gpsProvider = LOCATIOnManager.getProvider(LOCATIOnManager.GPS_PROVIDER);
        LOCATIOnProvider networkProvider = LOCATIOnManager.getProvider(LOCATIOnManager.NETWORK_PROVIDER);
        LOCATIOnProvider passiveProvider = LOCATIOnManager.getProvider(LOCATIOnManager.PASSIVE_PROVIDER);

        //figure out if we have a LOCATIOn somewhere that we can use as a current best LOCATIOn
        if( gpsProvider != null ) {
            LOCATIOn lastKNownGPSLOCATIOn = LOCATIOnManager.getLastKNownLOCATIOn(gpsProvider.getName());
            if( isBetterLOCATIOn(lastKNownGPSLOCATIOn,currentBestLOCATIOn) )
                currentBestLOCATIOn = lastKNownGPSLOCATIOn;
        }

        if( networkProvider != null ) {
            LOCATIOn lastKNownNetworkLOCATIOn = LOCATIOnManager.getLastKNownLOCATIOn(networkProvider.getName());
            if( isBetterLOCATIOn(lastKNownNetworkLOCATIOn,currentBestLOCATIOn) )
                currentBestLOCATIOn = lastKNownNetworkLOCATIOn;
        }

        if( passiveProvider != null) {
            LOCATIOn lastKNownPassiveLOCATIOn = LOCATIOnManager.getLastKNownLOCATIOn(passiveProvider.getName());
            if( isBetterLOCATIOn(lastKNownPassiveLOCATIOn,currentBestLOCATIOn)) {
                currentBestLOCATIOn = lastKNownPassiveLOCATIOn;
            }
        }

        gpsLOCATIOnListener = new serviceLOCATIOnListener();
        networkLOCATIOnListener = new serviceLOCATIOnListener();
        passiveLOCATIOnListener = new serviceLOCATIOnListener();

        if(gpsProvider != null) {
            LOCATIOnManager.requestLOCATIOnupdates(gpsProvider.getName(),0l,0.0f,gpsLOCATIOnListener);
        }

        if(networkProvider != null) {
            LOCATIOnManager.requestLOCATIOnupdates(networkProvider.getName(),networkLOCATIOnListener);
        }

        if(passiveProvider != null) {
            LOCATIOnManager.requestLOCATIOnupdates(passiveProvider.getName(),passiveLOCATIOnListener);
        }

        if(gpsProvider != null || networkProvider != null || passiveProvider != null) {
            handler.postDelayed(timerRunnable,2 * 60 * 1000);
        } else {
            handler.post(timerRunnablE);
        }
    } catch (SecurityException sE) {
        finish();
    }
}

private class serviceLOCATIOnListener implements android.LOCATIOn.LOCATIOnListener {

    @Override
    public void onLOCATIOnChanged(LOCATIOn newLOCATIOn) {
        synchronized ( this ) {
            if(isBetterLOCATIOn(newLOCATIOn,currentBestLOCATIOn)) {
                currentBestLOCATIOn = newLOCATIOn;

                if(currentBestLOCATIOn.hasAccuracy() && currentBestLOCATIOn.getAccuracy() <= 100) {
                    finish();
                }
            }
        }
    }

    @Override
    public void onStatusChanged(String s,int i,Bundle bundlE) {}

    @Override
    public void onProviderEnabled(String s) {}

    @Override
    public void onProviderDisabled(String s) {}
}

private synchronized void finish() {
    handler.removeCallBACks(timerRunnablE);
    handler.post(timerRunnablE);
}

/** Determines whether one LOCATIOn reading is better than the current LOCATIOn fix
 * @param LOCATIOn  The new LOCATIOn that you want to evaluate
 * @param currentBestLOCATIOn  The current LOCATIOn fix,to which you want to compare the new one
 */
protected Boolean isBetterLOCATIOn(LOCATIOn LOCATIOn,LOCATIOn currentBestLOCATIOn) {
    //etc
}

private Runnable timerRunnable = new Runnable() {

    @Override
    public void run() {
        Intent intent = new Intent(LOCATIOnservice.this.getPackagename() + ".action.LOCATION_FOUND");

        if(currentBestLOCATIOn != null) {
            intent.putExtra(LOCATIOnManager.KEY_LOCATION_CHANGED,currentBestLOCATIOn);

            LOCATIOnManager.removeupdates(gpsLOCATIOnListener);
            LOCATIOnManager.removeupdates(networkLOCATIOnListener);
            LOCATIOnManager.removeupdates(passiveLOCATIOnListener);
        }
    }
};

大佬总结

以上是大佬教程为你收集整理的location和currentBestLocation来自哪里?在Android开发指南(获取用户位置)全部内容,希望文章能够帮你解决location和currentBestLocation来自哪里?在Android开发指南(获取用户位置)所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。