2

我尝试获取 gps 数据。但是 gps 数据返回空值。当我查看时,以下“位置”返回 null,因此我无法获取 gps 数据。
位置 = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

我的代码如下;

    protected LocationManager locationManager;

public GPSTracker(Context context) {
    this.mContext = context;
    getLocation();
}

public Location getLocation() {
    try {
        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)
        {

        }
        else if (!isGPSEnabled && !isNetworkEnabled) 
        {
                        } 
        else 
        {
            this.canGetLocation = true;

            if (isGPSEnabled) {
                if (location == null) {
                    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                    Log.d("GPS Enabled", "GPS Enabled");
                    if (locationManager != null) {

                        location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                        if (location != null) {
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                        }
                    }
                }
            }

我等你的帮助。谢谢

4

2 回答 2

1

您是否在清单中设置了这些?

<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
于 2013-03-27T22:27:06.677 回答
1

我像下面的代码一样改变了,然后成功地工作了。

String bestProvider = locationManager.getBestProvider(criteria, false);
                
locationManager.requestLocationUpdates(bestProvider, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
    if (locationManager != null) {
        location = locationManager.getLastKnownLocation(bestProvider);
        if (location != null) {
            latitude = location.getLatitude();
            longitude = location.getLongitude();
        }
    }
}
于 2013-03-28T22:06:39.423 回答