0

我已经完成了这段代码,我要求更新位置。我想知道我是否正确实现了它。打开页面后,我请求更新(通过 gps 优先选择网络)...并在函数 onlocationchanged 中显示位置。我想问一下,当我做getlastknownlocation时,是否有可能在onlocationchanged中得到类似的位置空值?我停止使用 ghetlastknownlocation,因为它有时会返回 nulll 和错误的值。

      boolean isGPSEnabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
    boolean  isNetworkEnabled = lm
            .isProviderEnabled(LocationManager.NETWORK_PROVIDER);
    if (!isGPSEnabled && !isNetworkEnabled)
    {   
        ProgressBar progress = (ProgressBar)findViewById(R.id.progress);
        progress.setVisibility(View.INVISIBLE);
        Utilities.showAlertDialog2(RegionalStoresMeActivity.this, "", ""+getResources().getString(R.string.nothing_enabled), false);
    }
    else if(isNetworkEnabled){
        // lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,0, this);       
        lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,0, this);
    }
    else if(isGPSEnabled ) {                                                                                      
               lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,1,0, this);
    } 
4

1 回答 1

0

仅当它们是具有适当纬度和经度的位置更新时才调用 onLocationChanged() 方法。这可能是最微小的可能性,但我从未经历过。

你做得很好,但我想提出一个建议。考虑一个场景-

网络提供商已启用,并且您已请求位置更新。但由于某种原因,它不返回位置更新(在很多情况下,位置提供者不返回位置更新,也不是必须返回位置更新。)

现在根据您的代码,它不会在“else if(isGPSEnabled)”中检查 GPS 提供程序。所以我建议你只在下面的“if”语句中实现它

if( isNetworkEnabled )
    lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,0, this);

if( isGPSEnabled )                                                                                      
    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,1,0, this);
于 2014-03-27T07:03:40.173 回答