【问题标题】:[Android-Location]Can not get Longitude and Latitude from LocationManager[Android-Location]无法从LocationManager获取经纬度
【发布时间】:2023-03-21 18:58:02
【问题描述】:

我使用 LocationManager 通过“经度”和“纬度”获取用户地址,但是当我调用 location.getLongitude() 和 location.getLatitude() 时,它返回 0 给我。我设法使用 Google,但没有人遇到我的问题。

Jeremy Edwards 先生建议我:

确保在手机设置中启用了基于网络的位置信息。您可以检测到这种情况并在您确实需要时提示用户启用它。 我相信这是启动此活动的代码行。

我会这样做,但它可以提供帮助。有时给我经度和纬度,有时没有。

这是我的代码:

String locationProvider = LocationManager.NETWORK_PROVIDER;
LocationManager locationManager = (LocationManager) this
            .getSystemService(Context.LOCATION_SERVICE);
Location location = locationManager
            .getLastKnownLocation(locationProvider);

AndroidMainfest.xml 中的权限:

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

谁能帮帮我?提前谢谢!

【问题讨论】:

  • 你是在模拟器还是手机上试试?如果在模拟器中,则启用 DDMS 设置。如果在手机中,则启用带有街道数据设置的 GPS。
  • 我使用的是 Nexus 4,我会尝试通过街道数据设置启用 GPS。

标签: android locationmanager android-location


【解决方案1】:

您试图仅从网络提供商处获取。尝试使用下面的代码。它将帮助您从 GPS 提供商处获取,如果值不可用,那么它将帮助您从网络提供商处获取坐标

// The minimum distance to change Updates in meters
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters

// The minimum time between updates in milliseconds
private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute



        locationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE);

        // getting GPS status
        isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

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

        if (!isGPSEnabled && !isNetworkEnabled) {
            // no GPS Provider and no network provider is enabled
        } 
        else 
        {   // Either GPS provider or network provider is enabled

            // First get location from Network Provider
            if (isNetworkEnabled) 
            {
                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) 
                    {
                        latitude = location.getLatitude();
                        longitude = location.getLongitude();
                        this.canGetLocation = true;
                    }
                }
            }// End of IF network enabled

            // if GPS Enabled get lat/long using GPS Services
            if (isGPSEnabled) 
            {
                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) 
                    {
                        latitude = location.getLatitude();
                        longitude = location.getLongitude();
                        this.canGetLocation = true;
                    }
                }

            }// End of if GPS Enabled
        }// End of Either GPS provider or network provider is enabled

【讨论】:

  • 谢谢妮莎!它对我有用,但我想知道为什么我的代码 >Location location = locationManager.getLastKnownLocation(locationProvider);有时会起作用,有时会不起作用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-25
  • 1970-01-01
  • 2016-09-15
  • 1970-01-01
  • 2012-09-26
相关资源
最近更新 更多