融合定位提供程序似乎没有使用GPS接收器

39

Moto G上运行Android 4.3,Nexus 7 2012和Nexus 5上运行Android 4.4.2。使用的是Android Studio 0.4。

我不想接收定期位置更新,我只想在用户按下按钮时获得精确的位置。

我已经按照这个例子进行了操作:https://developer.android.com/training/location/retrieve-current.html

在清单文件中:

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

我使用GooglePlayServicesUtil.isGooglePlayServicesAvailable检查Play服务是否可用。

在主活动中:

//in activity onCreate method
mLocationClient = new LocationClient(this, this, this);

@Override
protected void onStart() {
    mLocationClient.connect();
    super.onStart();
}

@Override
protected void onStop() {
    mLocationClient.disconnect();
    super.onStop();
}

//in button onclick method    
mCurrentLocation = mLocationClient.getLastLocation();

我的手机没有SIM卡。如果我开启WiFi,有时候可以得到准确的位置信息。其他时候,mCurrentLocation为空。

如果我关闭WiFi,则mCurrentLocation始终为空。

我在外面测试了多个有天空视野的位置,每个位置等待了三分钟。

我从未看到GPS图标出现在Android通知栏顶部。

我的定位设置如下: enter image description here

一个GPS测试应用程序成功地在同一设备上禁用WiFi室内使用GPS,因此GPS是正常工作的: enter image description here

注册位置更新,如https://developer.android.com/training/location/receive-location-updates.html,也不起作用。注册方法从未被调用。

我做错了什么?


1
发布一些代码,GPS 获得适当的锁定也需要一些时间。 - tyczj
1
@tyczj 刚刚编辑了问题并添加了代码。我在每个位置等待了三分钟,但GPS图标都没有出现在Android通知栏中。 - cja
1
谢谢!你能否发布一下你正在使用的LocationRequest代码来注册位置更新?另外,我假设当你说你打开WiFi时也有互联网连接?你还可以使用我的GPS基准应用程序在这里测试融合提供者-https://play.google.com/store/apps/details?id=com.gpsbenchmark.android - Sean Barbeau
1
GPS基准测试看起来会非常有用。 - cja
3
你真的需要这两个权限吗?<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> - Marian Paździoch
显示剩余5条评论
12个回答

0

我更喜欢使用实现了 LocationListener 接口的服务来获取当前近似精确位置。我通常按照以下步骤进行:

  1. onCreate() 中初始化 LocationManager 并调用 requestLocationUpdates 来同时请求 GPS_PROVIDERNETWORK_PROVIDER 的位置更新。
  2. 分别调用 getLastKnownLocation 方法获取 GPS_PROVIDERNETWORK_PROVIDER 的最新位置,并使用 getTime() 方法了解最新位置的时间戳。
  3. 广播上一个位置(如果它在 30 秒内)(可选)。
  4. onLocationChanged 方法被调用时,将新位置与上一个最佳位置进行比较,并检查定位精度。
  5. 如果精度差小于 MIN_ACCURACY(用户变量),则将其作为最佳位置。
  6. 广播当前最佳位置。
  7. 必须删除 LocationManagerlocationManager.removeUpdates(this);
  8. 调用 stopSelf();(也可以从 activity 的 onDestroy 方法中停止服务)。

编辑:

好的...上面的方法是使用位置API,下面我已经编写了使用Fused Provider(GooglePlayServices)的代码。我已经在我的Nexus 5(Android 4.4.2),没有SIM卡,没有WIFI的情况下进行了测试...并且我得到了结果。

编辑2:我还在Android 2.3.3 LG Optimus(没有SIM卡和Wifi)上进行了测试,使用Fused Provided需要大约5分钟才能锁定位置,但我立即获得了位置图标。

public class HomeActivity extends FragmentActivity implements
        ActionBar.OnNavigationListener,
        GooglePlayServicesClient.ConnectionCallbacks,
        GooglePlayServicesClient.OnConnectionFailedListener, LocationListener {

    private LocationClient locationClient;
    private LocationRequest locationRequest;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_home);
            // ...
            int resp = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
        if (resp == ConnectionResult.SUCCESS) {
            locationClient = new LocationClient(this, this, this);
            locationClient.connect();
        } else {
            Toast.makeText(this, "Google Play Service Error " + resp,
                    Toast.LENGTH_LONG).show();

        }
    }

    @Override
    public void onConnected(Bundle connectionHint) {
        if (locationClient != null && locationClient.isConnected()) {

            locationRequest = LocationRequest.create();
            locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
            locationRequest.setInterval(100);
            locationClient.requestLocationUpdates(locationRequest, this);

        }
    }

    @Override
    public void onLocationChanged(Location location) {

        try {
            if (location != null) {
                LatLng gps = new LatLng(location.getLatitude(), location.getLongitude());
                Log.v("JD", "My Location: " + gps.toString());
            }
        } catch (Exception e) {
            Log.d("JD",
                    "onLocationChanged", e);

        }       
    }
}

3
这与谷歌Play服务中的融合定位API无关。 - tyczj
上述方法适用于我的应用程序,无需SIM卡和Wifi,我认为这可能对OP更有用。无论如何,我将使用Fused提供程序检查并编辑我的帖子。 - Tamal
以上代码没有使用融合定位提供程序,我们是否可以在不打开设置中的位置的情况下从融合提供程序获取位置。 - Srishti Roy

0

由于没有人分享这个链接,我发现这是最有帮助的文档 http://developer.android.com/guide/topics/location/strategies.html

"你可能认为最近的位置修复是最准确的。然而,由于位置修复的准确性不同,最近的修复并不总是最好的。您应该根据几个标准选择位置修复的逻辑。这些标准也因应用程序的使用情况和现场测试而异。"

最好的方法是在活动处于前台时保持位置监听器,并在按下按钮时选择最准确的缓存位置。您可能需要显示旋转器或其他内容,并在等待准确测量结果时禁用按钮。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接