1

I have very interesting problem, My Location manager sometimes working, sometimes returning null data.

I check all permission , gradle files options and runtime check about location is enabled. My phone not have airplane mode and I do not off location.

Whatever I check all data, There is no trouble for the get the location data.

Main Code

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

           }
       };
Berat Eyüboğlu
  • 1,663
  • 3
  • 18
  • 32
  • 1
    Asked on SO so many times: [getLastKnownLocation return NULL using GPS_PROVIDER and NETWORK_PROVIDER](http://stackoverflow.com/questions/36747349/getlastknownlocation-return-null-using-gps-provider-and-network-provider), [getLastKnownLocation returning NULL](http://stackoverflow.com/questions/19621882/getlastknownlocation-returning-null), [getLastKnownLocation returns null](http://stackoverflow.com/questions/20438627/getlastknownlocation-returns-null),... just to mention some. Search first next time! – nvi9 Mar 17 '17 at 08:18
  • yeah searched so many times, dont work this link ok? – Berat Eyüboğlu Mar 17 '17 at 08:19
  • Have you tried to change the 1000 ms' to 0 in `mLocationManager.requestLocationUpdates` and move the line `location = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);` after it? – nvi9 Mar 17 '17 at 08:33
  • All the links work and the first one tries to explain the problem with `getLastKnownLocation()` in more detail. Or is your problem that you actually get `null` locations in `onLocationChanged()`? That shouldn't happen but I've seen it sometimes, so I just check the `Location` object there and ignore it if it's `null`. – Markus Kauppinen Mar 17 '17 at 09:13

0 Answers0