location和currentBestLocation来自哪里?在Android开发指南(获取用户位置)

location和currentBestLocation来自哪里?在Android开发指南(获取用户位置),第1张

概述我在 Android Dev Guid中阅读了关于 Obtaining User Location的教程, 我尝试将其改编为以下代码..但我不知道我应该将哪个位置值放入isBetterLocation(位置位置,位置currentBestLocation) Example.class private LocationManager locman; @Override 我在 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开发指南(获取用户位置)所遇到的程序开发问题。

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

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1129714.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-30
下一篇2022-05-30

发表评论

登录后才能评论

评论列表(0条)

    保存