GPS 和网络的标准

发布于 2024-10-07 22:29:07 字数 662 浏览 4 评论 0原文

我想知道如何从最佳提供商处获取位置
我是否必须制定两个单独的标准 1 用于 GPS,1 用于网络,还是有办法将它们全部放在一起?
这是我的代码,当我添加粗略标准时,GPS 无法运行(屏幕顶部没有 GPS 闪烁徽标),而当我使用精细标准时,我没有从网络获得任何东西......那么我是否必须为两者编写标准并在可用的条件之间进行切换,或者它们是否可以采用相同的标准?
因为我有“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);

I would like to know how can I get location from the best provider
do I have to make two separate criteria 1 for the GPS and 1 for the network or is there a way to put them all together ?
this is my code when I add the COARSE criteria the GPS does not go on (no GPS flashing logo on the top of the screen) and when I use the FINE criteria I dont get any thing from the network.......so do I have to write criteria for both and switch between them for what ever is available or can they both be in the same criteria ?
because I have the "getBestProvider(criteria, true);" in my code so it should get the location from the best provider....right..??!

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

花海 2024-10-14 22:29:07

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

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

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

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

Using a getBestProvider with Criteria.ACCURACY_FINE will never return the NETWORK_PROVIDER, even though wifi position is usually quite accurate.

In my application, I use getProviders to get all providers matching my criteria, then add the NETWORK_PROVIDER if it is activated and not yet in the list.

Then I launch a requestLocationUpdates for each of these providers, making sure to call removeUpdates when I get a location accurate enough.

This way, if the location provided by the network is accurate enough, the gps provider will be turned off

久而酒知 2024-10-14 22:29:07

如果未启用 gps,带有 Criteria.ACCURACY_FINE 的 @nicopico getBestProvider 可以返回 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 实现

@nicopico getBestProvider with Criteria.ACCURACY_FINE can return the NETWORK_PROVIDER if gps is not enabled
i use the following code in my app

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
}
}

this code is in a class that implements locationListener
you can edit this line

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

to reflect your implementation of locationListener

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文