Android位置管理器返回空位置数据

时间:2017-03-17 08:01:04

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

我有一个非常有趣的问题, 我的位置管理器有时会工作,有时会返回空数据。

我检查了所有权限,gradle文件选项和关于位置的运行时检查已启用。我的手机没有飞机模式,我没有离开位置。

无论我检查所有数据,获取位置数据都没有问题。

主要代码

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);

        cs = (ArrayList<HashMap<String, String>>) getIntent().getSerializableExtra("verigeldi");

         mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
         location = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);


        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            return; // check permission
        }
        mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000,1000, mLocationListener);


    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

try { // THIS AREA VERY IMPORTANT !!!!
    longitude = location.getLongitude(); // GET NULL POINTER EXCEPTION SOMETIMES WORKING
    latitude = location.getLatitude(); // GET NULL POINTER EXCEPTION SOME TIMES WORKING
    LatLng latLng = new LatLng(latitude, longitude);
    CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, 13);
    mMap.animateCamera(cameraUpdate);
            }

catch (Exception e) {
    LatLng latLng = new LatLng(39.052540, 35.410083); 
    CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, 5);
    mMap.animateCamera(cameraUpdate);
    }



        checkLocationIsEnabled();



        googleMap.getUiSettings().setMyLocationButtonEnabled(true);
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            return;
        }
        googleMap.setMyLocationEnabled(true);
    }


       public final LocationListener mLocationListener = new LocationListener() {
           @Override
           public void onLocationChanged(Location location) {
               LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
               CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, 13);
               mMap.animateCamera(cameraUpdate);
               mLocationManager.removeUpdates(this);
           }

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


           }

           @Override
           public void onProviderEnabled(String provider) {


           }

           @Override
           public void onProviderDisabled(String provider) {

           }
       };

0 个答案:

没有答案