无法使用GPS_PROVIDER获取位置

时间:2016-02-15 16:52:04

标签: android gps location

我正在开发一款应用程序,其中我需要使用GPS的用户位置。我在位置对象而不是坐标中得到空值。但是在使用NETWORK_PROVIDER时我得到了结果。我希望使用GPS获得相同的结果。

这是我的代码:

import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

import static com.google.android.gms.common.GooglePlayServicesUtil.isGooglePlayServicesAvailable;
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback,LocationListener {

private GoogleMap mMap;
boolean isGPSEnabled = false;
Location location;
double latitude;
double longitude;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_maps);
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
    int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());
    if(status == ConnectionResult.SUCCESS) {
        System.out.println("enable");
        System.out.println(status);
    }
   // mMap.setMyLocationEnabled(true);
    try {

        LocationManager locationManager = (LocationManager)this.getSystemService(this.LOCATION_SERVICE);
        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_FINE);
        criteria.setPowerRequirement(Criteria.POWER_HIGH);
        criteria.setAltitudeRequired(false);
        criteria.setBearingRequired(false);
        criteria.setSpeedRequired(false);
        criteria.setCostAllowed(true);
        isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
       // LocationProvider BestProvider = locationManager.getProvider(LocationManager.GPS_PROVIDER);
        if (isGPSEnabled) {
            System.out.println("test5");
            location =locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000,1, this);
            System.out.println(locationManager);
            if (locationManager != null) {
                System.out.println(location);
                //requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                if (location != null) {
                    latitude = location.getLatitude();
                    longitude = location.getLongitude();
                    System.out.println("latitude:"+latitude);
                }
            }
        }
    }
    catch(Exception e) {
        System.out.println("Fail");
        e.printStackTrace();
    }
}

1 个答案:

答案 0 :(得分:0)

名为location的位置对象始终为空,因为在此检查完成之前,它从未在任何位置分配过值

if (location != null)

您的意思是为它分配这样的值吗?

location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

另请阅读LocationManager javadoc中的说明,了解从GPS提供商处获取结果需要保留哪些权限。您将需要ACCESS_FINE_LOCATION。

最后,请注意GPS可能需要一些时间来获取准确数据的锁定。使用getLastKnownLocation时,无法保证数据立即可用。您可能必须等待第一个位置更新。在这种情况下,您应该使用侦听器等待第一个准确的位置。

另请参阅official docs以获取有关使用位置提供商的最佳信息。