哪里的位置和currentBestLocation从何而来?在Android开发人员指南(获取用户位置) [英] Where do the location and currentBestLocation come from? in Android Dev Guide (Obtaining User Location)

查看:192
本文介绍了哪里的位置和currentBestLocation从何而来?在Android开发人员指南(获取用户位置)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读到获取用户位置教程在Android开发的Guid和, 我努力去适应这个以下code ..但我不知道是哪个位置的价值,我应该投入 isBetterLocation(地点位置,位置currentBestLocation)

Example.class

 私人LocationManager locman;

        @覆盖
        保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);

            字符串情形= Context.LOCATION_SERVICE;
            locman =(LocationManager)getSystemService(上下文);

            标准标准=新标准();
            criteria.setAccuracy(Criteria.ACCURACY_FINE);
            criteria.setAltitudeRequired(假);
            criteria.setBearingRequired(假);
            criteria.setPowerRequirement(Criteria.POWER_LOW);

            字符串商= locman.getBestProvider(标准,真正的);
            locman.requestLocationUpdates(
                    供应商,MIN_TIME,MIN_DISTANCE,LocationListener的);

        }

        私人LocationListener的LocationListener的=新LocationListener的(){

        @覆盖
        公共无效onLocationChanged(位置定位){
            //我应该怎么通过在该方法中第一和第二个参数
            如果(isBetterLocation(LOCATION1,地点2)){
               // isBetterLocation =真>做updateLocation
               updateLocation(位置);
            }
        }

        @覆盖
        公共无效onProviderDisabled(字符串提供商){}
        @覆盖
        公共无效onProviderEnabled(字符串提供商){}
        @覆盖
        公共无效onStatusChanged(字符串商,INT地位,捆绑演员){}

       };

     保护布尔isBetterLocation(地点位置,位置currentBestLocation){
         如果(currentBestLocation == NULL){
            //一个新的位置总是比没有位置更好
            返回true;
         }
          //简介...查看code在Android开发的Guid获取用户位置
     }
 

解决方案

它并不难,真的。什么是code的作用是接收上找到位置不断更新;可以有多个听众听不同的提供者,因此这些更新可以是更多或更少的精确取决于提供商(GPS例如可以比网络更准确)。 isBetterLocation(...)评估是否由听众发现了一个位置实际上是比你已经知道了(应该有一个参考的code更好)。该isBetterLocation(...)code是有据可查的,所以它不应该是不难理解的,但第一个参数位置是由供应商找到了新的位置,和 currentBestLocation 是你已经知道的位置。

在code我用的是关于你的一样,但我不只是拿最好的供应商。 该处理的东西,因为我不想继续更新,只要找到最佳的位置,准确的说是足以让我在两分钟的时间内最大(全球定位系统可以采取一些)。

 私人地点currentBestLocation = NULL;
私人ServiceLocationListener gpsLocationListener;
私人ServiceLocationListener networkLocationListener;
私人ServiceLocationListener passiveLocationListener;
私人LocationManager locationManager;

私人处理程序处理程序=新的处理程序();


公共无效fetchLocation(){
    locationManager =(LocationManager)this.getSystemService(Context.LOCATION_SERVICE);

    尝试 {
        LocationProvider gpsProvider = locationManager.getProvider(LocationManager.GPS_PROVIDER);
        LocationProvider networkProvider = locationManager.getProvider(LocationManager.NETWORK_PROVIDER);
        LocationProvider passiveProvider = locationManager.getProvider(LocationManager.PASSIVE_PROVIDER);

        //计算出,如果我们有一个位置的地方,我们可以作为一个目前最好的位置使用
        如果(gpsProvider!= NULL){
            位置lastKnownGPSLocation = locationManager.getLastKnownLocation(gpsProvider.getName());
            如果(isBetterLocation(lastKnownGPSLocation,currentBestLocation))
                currentBestLocation = lastKnownGPSLocation;
        }

        如果(networkProvider!= NULL){
            位置lastKnownNetworkLocation = locationManager.getLastKnownLocation(networkProvider.getName());
            如果(isBetterLocation(lastKnownNetworkLocation,currentBestLocation))
                currentBestLocation = lastKnownNetworkLocation;
        }

        如果(passiveProvider!= NULL){
            位置lastKnownPassiveLocation = locationManager.getLastKnownLocation(passiveProvider.getName());
            如果(isBetterLocation(lastKnownPassiveLocation,currentBestLocation)){
                currentBestLocation = lastKnownPassiveLocation;
            }
        }

        gpsLocationListener =新ServiceLocationListener();
        networkLocationListener =新ServiceLocationListener();
        passiveLocationListener =新ServiceLocationListener();

        如果(gpsProvider!= NULL){
            locationManager.requestLocationUpdates(gpsProvider.getName(),0L,0.0,gpsLocationListener);
        }

        如果(networkProvider!= NULL){
            locationManager.requestLocationUpdates(networkProvider.getName(),0L,0.0,networkLocationListener);
        }

        如果(passiveProvider!= NULL){
            locationManager.requestLocationUpdates(passiveProvider.getName(),0L,0.0,passiveLocationListener);
        }

        如果(gpsProvider!= NULL || networkProvider!= NULL || passiveProvider!= NULL){
            handler.postDelayed(timerRunnable,2 * 60 * 1000);
        } 其他 {
            handler.post(timerRunnable);
        }
    }赶上(SecurityException异常SE){
        完();
    }
}

私有类ServiceLocationListener实现android.location.LocationListener {

    @覆盖
    公共无效onLocationChanged(位置newLocation){
        同步(本){
            如果(isBetterLocation(newLocation,currentBestLocation)){
                currentBestLocation = newLocation;

                如果(currentBestLocation.hasAccuracy()&安培;&安培; currentBestLocation.getAccuracy()&其中; = 100){
                    完();
                }
            }
        }
    }

    @覆盖
    公共无效onStatusChanged(字符串S,INT I,束束){}

    @覆盖
    公共无效onProviderEnabled(String s)将{}

    @覆盖
    公共无效onProviderDisabled(String s)将{}
}

私人同步无效结束(){
    handler.removeCallbacks(timerRunnable);
    handler.post(timerRunnable);
}

/ **确定一个位置读数是否比当前位置锁定更好
 * @参数的位置,你要评估新位置
 *参数currentBestLocation目前的位置锁定,您要比较新的这
 * /
保护布尔isBetterLocation(地点位置,位置currentBestLocation){
    //等等
}

私人可运行timerRunnable =新的Runnable(){

    @覆盖
    公共无效的run(){
        意向意图=新的意图(LocationService.this.getPackageName()+.action.LOCATION_FOUND);

        如果(currentBestLocation!= NULL){
            intent.putExtra(LocationManager.KEY_LOCATION_CHANGED,currentBestLocation);

            locationManager.removeUpdates(gpsLocationListener);
            locationManager.removeUpdates(networkLocationListener);
            locationManager.removeUpdates(passiveLocationListener);
        }
    }
};
 

I read the tutorial about Obtaining User Location in Android Dev Guid and, I try to adapt this to the following code.. but i don't know which location value I should put into isBetterLocation(Location location, Location 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"
     }

解决方案

Its not that hard really. What the code does is it receives continuous updates on locations found; you can have multiple listeners listening to different providers and as such those updates can be more or less accurate depending on the provider (GPS for example could be more accurate than network). isBetterLocation(...) evaluates if a location found by the listener is actually better than the one you already know about (and should have a reference to in your code). The isBetterLocation(...) code is well documented, so it shouldn't be hard to understand, but the first parameter location is the new location found by a provider, and currentBestLocation is the location you already know about.

The code I use is about the same as yours, except I don't just take best provider. The handler stuff is because I don't want continued updates, just find the best possible location that is accurate enough for me within a maximum timeframe of two minutes (GPS can take a bit).

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(), 0l, 0.0f, networkLocationListener);
        }

        if(passiveProvider != null) {
            locationManager.requestLocationUpdates(passiveProvider.getName(), 0l, 0.0f, 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);
        }
    }
};

这篇关于哪里的位置和currentBestLocation从何而来?在Android开发人员指南(获取用户位置)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆