【问题标题】:Android GPS Tracking issueAndroid GPS 追踪问题
【发布时间】:2014-05-12 16:28:32
【问题描述】:

我试图为我的 android 应用程序获取位置,但 getLastKnownLocation 总是返回 null 并且永远不会调用 onLocationChanged。我对我的应用使用以下权限:

ACCESS_NETWORK_STATE, 
ACCESS_FINE_LOCATION ACCESS_COARSE_LOCATION. 

GPS 和互联网已打开,我的设备已打开。我还尝试了 requestLocationUpdates 的不同值。

        locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        locationManager.removeUpdates(this);

        isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

        // getting network status
        isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

        if (!isGPSEnabled && !isNetworkEnabled) 
        {
            // no network provider is enabled
        } 
        else 
        {
            hasGPS      = true;
            location    = null;
            if (isGPSEnabled) 
            {
                if (location == null) 
                {
                    locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER,
                            MIN_TIME_BW_UPDATES,
                            MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                    if (locationManager != null) 
                    {
                        location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                        if (location != null) 
                        {
                            lat = location.getLatitude();
                            lng = location.getLongitude();
                        }
                    }
                }
            }

            if (isNetworkEnabled && location == null) 
            {
                locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
                        MIN_TIME_BW_UPDATES,
                        MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                if (locationManager != null) 
                {
                    location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                    if (location != null) 
                    {
                        lat = location.getLatitude();
                        lng = location.getLongitude();
                    }
                }
            }
        }

【问题讨论】:

  • 这听起来可能很傻,但您是在实际的手机上正确地测试它(即,不使用模拟器)?
  • 可能对herehere有帮助的相关信息。
  • 是的,我在装有 android 4.1 的平板电脑上尝试了此代码
  • 安装一个知名的 Android 应用程序,您可以在其中查看 GPS 是否正常工作。顺便说一句,您的位置更新监听器在哪里?

标签: java android gps


【解决方案1】:

如果 GetLastKnownLocation 从未通过该提供程序获得过位置,或者该位置太旧,则 GetLastKnownLocation 将返回 null。所以这并不意外。很可能您没有收到 GPS 信号,这可能需要几秒钟到几分钟的时间(或者在许多建筑物中完全失败)。仔细检查 GPS 是否已在设置中打开,并查看通知区域中闪烁的圆圈 - 如果它从未变亮,则说明您没有收到信号。

【讨论】:

  • 感谢您的提示,圆圈永远不会变实。我现在就去外面试试。
【解决方案2】:

GPS 信号无法穿透墙壁,因此通常无法在建筑物内工作。

出于开发和测试目的,您可以使用 Android 模拟位置: http://developer.android.com/training/location/location-testing.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多