GPS和网络的标准

时间:2010-12-17 07:00:48

标签: android gps location

我想知道如何从最佳提供商处获取位置 我是否必须为GPS制定两个单独的标准1,为网络制定1个标准,还是有办法将它们放在一起? 这是我的代码,当我添加GPS没有进行的COARSE标准时(屏幕顶部没有GPS闪烁标志),当我使用FINE标准时,我从网络上得不到任何东西.......那么我是否必须为两者编写标准并在它们之间进行切换以获得可用的标准,或者它们是否都符合相同的标准? 因为我有“getBestProvider(标准,真实);”在我的代码中所以它应该从最好的提供者获取位置....对.. ??!

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);

2 个答案:

答案 0 :(得分:1)

getBestProvider使用Criteria.ACCURACY_FINE将永远不会返回NETWORK_PROVIDER,即使wifi位置通常非常准确。

在我的应用程序中,我使用getProviders来获取符合我的条件的所有提供商,然后添加NETWORK_PROVIDER(如果它已激活但尚未列在列表中)。

然后我为每个提供商启动requestLocationUpdates,确保在我获得足够准确的位置时致电removeUpdates

这样,如果网络提供的位置足够准确,gps提供商将被关闭

答案 1 :(得分:0)

带有Criteria.ACCURACY_FINE的@nicopico 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

的实现