【问题标题】:Can't get location from Android无法从 Android 获取位置
【发布时间】:2014-08-08 12:50:32
【问题描述】:

我正在尝试获取我的设备的位置,但在多次运行代码后只成功了一次。以下是我正在使用的代码:

    final LocationListener mLocationListener = new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {
            Log.d(TAG, location.getLatitude() + ", " + location.getLongitude());
        }

        @Override
        public void onStatusChanged(String s, int i, Bundle bundle) {
            Log.d(TAG, "changed: " + s);
        }

        @Override
        public void onProviderEnabled(String s) {
            Log.d(TAG, "enabled: " + s);
        }

        @Override
        public void onProviderDisabled(String s) {
            Log.d(TAG, "disabled: " + s);
        }
    };

    LocationManager mLocationManager = (LocationManager) getActivity().getSystemService(getActivity().getApplicationContext().LOCATION_SERVICE);

    boolean enabled = mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
    Log.d(TAG, "gps enabled?" + enabled);
    Log.d(TAG, "network enabled?" +  mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER));

    Criteria criteria = new Criteria();
    best = mLocationManager.getBestProvider(criteria, true);

    Log.d(TAG, best);
    Log.d(TAG, LocationManager.GPS_PROVIDER);
    Log.d(TAG, LocationManager.NETWORK_PROVIDER);

    mLocationManager.requestLocationUpdates(best, 0, 0, mLocationListener);

我在室内(我希望它在室内工作),所以我不断将 best 设置为 networkgps enablednetwork enabled 字符串都打印出 true。是我获取位置不一致的原因,还是我可以做些什么来解决这个问题,以便我可以在室内获取位置?

【问题讨论】:

  • 您应该使用 Fused 提供程序 (developer.android.com/google/play-services/location.html)
  • 如果您更喜欢在室内使用它,为什么还要使用 GPS?无论哪种方式,都只向任何一个提供商提出请求,所以我认为这种不一致的可能性不大。
  • @SoothSayer 我现在按照您的建议使用 Fused 提供程序,它运行良好。如果你愿意,你可以发表你的评论作为答案,或者我会删除问题

标签: android android-location


【解决方案1】:

因此,对于设备位置,使用 Fused 提供程序可以获得更好的结果。

此外,您无需管理不同的提供商。

Android developer documentation about location

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-09-19
    • 1970-01-01
    • 1970-01-01
    • 2016-09-24
    • 2021-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多