GPS和网络的标准是什么?

3
我想知道如何从最佳提供商那里获取位置信息。我需要分别为GPS和网络设置两个不同的条件吗?还是有一种方法可以将它们合并在一起?这是我的代码,当我添加COARSE条件时,GPS不会打开(屏幕顶部没有GPS闪烁标志),而当我使用FINE条件时,我无法从网络中获取任何信息......所以我需要为两者编写条件,并在可用时切换它们,还是它们可以在同一个条件下?因为我的代码中有“getBestProvider(criteria, true);”,所以它应该从最佳提供商那里获取位置...对吗?
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
//// criteria.setAccuracy(Criteria.ACCURACY_COARSE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_HIGH);
String provider = locationManager.getBestProvider(criteria, true);

你为什么要调用 setAccuracy(),既然你显然愿意接受任何精度? - CommonsWare
我想要精确的定位,但在一些地方,比如隧道或高楼周围,唯一能获取到的定位信息就是来自网络。 - moe
2个回答

1

使用Criteria.ACCURACY_FINEgetBestProvider将永远不会返回NETWORK_PROVIDER,即使wifi位置通常相当准确。

在我的应用程序中,我使用getProviders获取与我的标准匹配的所有提供程序,然后如果已激活且尚未在列表中,则添加NETWORK_PROVIDER

然后,我为这些提供程序中的每一个启动requestLocationUpdates,确保在获得足够准确的位置时调用removeUpdates

这样,如果网络提供的位置足够准确,则关闭GPS提供程序。


0
@nicopico 使用 Criteria.ACCURACY_FINE 的 getBestProvider 可以在 GPS 未启用的情况下返回 NETWORK_PROVIDER。我在我的应用程序中使用以下代码。
public void getLocation() {
        // Getting Google Play availability status
        int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(context);

//        boolean network_enabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

        // Showing status
        if (status != ConnectionResult.SUCCESS) { // Google Play Services are not available
            Log.e(TAG, "getLocation fired Google Play Services are not available");

           mHandler.post(new UiToastCommunicaton(context.getApplicationContext(),
                   context.getResources().getString(R.string.gpserv_notfound)));
        }

        // Getting LocationManager object from System Service LOCATION_SERVICE
        locationManager = (LocationManager) context.getSystemService(IntentService.LOCATION_SERVICE);
        if (!locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {//check if network location provider context on
            Log.e(TAG, "network location provider not enabled");
        } else {
            criteria = new Criteria();
            criteria.setAccuracy(Criteria.ACCURACY_FINE);
            criteria.setAltitudeRequired(false);
            criteria.setBearingRequired(false);
            criteria.setCostAllowed(true);
            //criteria.setPowerRequirement(Criteria.POWER_LOW);
            // Getting the name of the best provider
            provider = locationManager.getBestProvider(criteria, true);

            // Check provider exists then request for location
            if (provider != null) {
                requestLocation(locationManager, provider);
            } else {
                //start wifi, gps, or tell user to do so
            }

        }
    }
public void requestLocation(LocationManager locationManager, String provider) {
Log.e(TAG, "requestLocation fired getting location now");

if (provider != null) {

    locationManager.requestLocationUpdates(provider, 0, 0, this);
    //locationManager.requestLocationUpdates(provider, 0, 0, this, Looper.getMainLooper());
} else {
    //tell user what has happened
}
}

这段代码位于实现locationListener接口的类中,你可以编辑这一行

locationManager.requestLocationUpdates(provider, 0, 0, this);

以反映您对locationListener的实现


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