Search code examples
javaandroidgoogle-mapsgeolocationlocationmanager

Problems displaying a location and centering the camera on it


I've just had some issues for odd reasons with the CameraUpdateFactory.

So I incorporated this code in an onclickListener on the GpsButton on a navigation fragment:

 if (ContextCompat.checkSelfPermission(mapFragment.getActivity(), android.Manifest.permission.ACCESS_FINE_LOCATION) ==
                    PackageManager.PERMISSION_GRANTED) {
                Log.d("Permission checked", "checkSelfPermission passed with no errors");
                map.setMyLocationEnabled(true);
                Log.d("Permission checked", "Location Layer implementation successful");
            } else {
                //Request the Permission
                ActivityCompat.requestPermissions(mapFragment.getActivity(), new String[]{
                        Manifest.permission.ACCESS_FINE_LOCATION}, 1);
            }

This basically enables the location to be displayed as a blue dot on the map only when the GPS button is pressed. This has been perfectly functional so far.

I also incorporated a method to move the camera to my current location:

 public void locateMe() {

    LocationManager locationManager = (LocationManager) getActivity().getSystemService(LOCATION_SERVICE); // Getting LocationManager object from System Service LOCATION_SERVICE
    Criteria criteria = new Criteria();// Creating a criteria object to retrieve provider
    String provider = locationManager.getBestProvider(criteria, true);// Getting the name of the best provider
    if (ContextCompat.checkSelfPermission(mapFragment.getActivity(), android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(mapFragment.getActivity(), new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, 2);
    }
    Location location = locationManager.getLastKnownLocation(provider);

    if (location != null) {

        double latitude = location.getLatitude(); //Getting latitude of the current location
        double longitude = location.getLongitude(); // Getting longitude of the current location
        myPosition = new LatLng(latitude, longitude); // Creating a LatLng object for the current location
        map.moveCamera(CameraUpdateFactory.newLatLngZoom(myPosition, CAMPUS_DEFAULT_ZOOM_LEVEL));//Camera Update method
    }
}

For some reason, this is hit or miss. 3 days ago, it was locking on a position that wasn't where I currently was located while for the past 2 days it worked perfectly. No code was changed whatsoever. Could someone please explain what's going on? Any fixes or suggestions would be appreciated.


Solution

  • This occurs because you are using getLastKnownLocation.

    This can be done without starting the provider. Note that this location could be out-of-date, for example if the device was turned off and moved to another location. If the provider is currently disabled, null is returned.

    documentation

    You have to use requestLocation updates if you want to retrieve the user's current documentation. requestLocationUpdates documentation