Android 6.0访问精确位置

时间:2016-01-29 14:18:17

标签: android location

我有这个代码来获取位置。问题不在于许可。

   public Location getLocation() {
    Location location = null;
    LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

    }
    location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
    return location;
}

这就是我获取位置的方式。

    lat = location.getLatitude();
    lng = location.getLongitude();

    LatLng myLocation = new LatLng(lat, lng);

错误讯息:

java.lang.NullPointerException: Attempt to invoke virtual method 'double android.location.Location.getLatitude()' on a null object reference

我迷失了。请问有什么不对?

0 个答案:

没有答案