【问题标题】:Why is my location null? - Android为什么我的位置为空? - 安卓
【发布时间】:2013-06-13 15:45:48
【问题描述】:

为什么我的位置变量的值为空?我正在正确设置我的 LocationManager。

这是我的源代码:

    //Get the current location
    locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    myCriteria = new Criteria();
    String provider = locationManager.getBestProvider(myCriteria, true);
    Location location = locationManager.getLastKnownLocation(provider);
    boolean isGpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
    boolean isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER); 
    //Location location;

    if (isGpsEnabled) { 
        location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    }
    else { 
        location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
    }

    Log.i(TAG, "Provider is: "+provider);
    Log.i(TAG, "Location is: "+location);

这是 logcat 的输出:

06-17 10:40:52.637: D/dalvikvm(15106): GC_FOR_ALLOC freed 534K, 7% free 10586K/11360K, paused 16ms, total 16ms
06-17 10:40:52.677: I/OGT.RideTracking(15106): Provider is: gps
06-17 10:40:52.677: I/OGT.RideTracking(15106): Location is: null
06-17 10:40:52.677: D/AndroidRuntime(15106): Shutting down VM

我的代码有什么问题?

【问题讨论】:

  • 不保证getLastKnownLocation为您提供位置。如果是null,您应该调用locationManager.requestLocationUpdates() 并等待提供者获取位置。
  • 您是否设置了 ACCESS_FINE_LOCATION 和 ACCESS_COARSE_LOCATION 权限?
  • 是的,我在 Manifest 文件中确实拥有这些权限
  • 您可能会得到空值,因为当前没有 lastKnownLocation。打开谷歌地图或使用 GPS 的东西,它可能会工作

标签: android android-location android-maps-v2


【解决方案1】:

试试这个代码

这些变量是类变量

LocationManager locManager;
LocationListener locListener;

create方法上添加此代码

locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locListener);

在同一文件中添加此代码

public class MyLocationListener implements LocationListener {

    public void onLocationChanged(Location loc) {
        Log.d("tag", "Finding Latitude");
        double lat = loc.getLatitude();
        Log.d("tag", "Lat: " + String.valueOf(lat));
        Log.d("tag", "Finding Longitude");
        double lon = loc.getLongitude();
        Log.d("tag", "Lon: " + String.valueOf(lon));
        Log.i(Tag, loc.toString());
        String Text = "Lat: " + lat + "\nLon: " + lon;

        // Display location
        locManager.removeUpdates(locListener);
    }

    public void onProviderDisabled(String provider) {

    }

    public void onProviderEnabled(String provider) {

    }

    public void onStatusChanged(String provider, int status, Bundle extras) {

    }
}

AndroidManifest.xml 需要

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

【讨论】:

    【解决方案2】:
    // getting GPS status
    isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
    

    检查 gps 是否启用。

    【讨论】:

    • 但是你调用Location location = locationManager.getLastKnownLocation(provider);不检查是否启用了 gps。
    猜你喜欢
    • 2022-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-08
    • 2017-12-04
    • 2013-11-17
    • 2013-05-20
    相关资源
    最近更新 更多