Hi I am developing android application in which I want users current location which good enough accurate. So i have 2 options to implement this one
Retrieving the Current Location. http://developer.android.com/training/location/retrieve-current.html
and
Receiving Location Updates http://developer.android.com/training/location/receive-location-updates.html
So here is my problem. I gone through Retrieving the Current Location and I realize at the end it gives me users last best known location. But my requirement is users current location.
So I move towards the Receiving Location Updates. I though I can get first update and i will stop updates. But here also at first time it gives me last location not new one. and after first reading it start gps to give successive updates. But I am interested in first update only.
So I need good solution which will give me users accurate current location. Either using gps or network or wifi any one which is available.
If I am doing anything wrong please correct. Need Help. thank you.
So this is how I get the users current location to use for my Google maps.
if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED || ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
            mMap.setMyLocationEnabled(true);
            Criteria criteria = new Criteria();
            LocationManager locationManager = (LocationManager)getActivity().getSystemService(Context.LOCATION_SERVICE);
            String provider = locationManager.getBestProvider(criteria, false);
            Location location = locationManager.getLastKnownLocation(provider);
            double lat =  location.getLatitude();
            double lng = location.getLongitude();
            LatLng coordinate = new LatLng(lat, lng);
            CameraUpdate yourLocation = CameraUpdateFactory.newLatLngZoom(coordinate, 13);
            mMap.animateCamera(yourLocation);
        } else {
            final AlertDialog alertDialogGPS = new AlertDialog.Builder(getActivity()).create();
            alertDialogGPS.setTitle("Info");
            alertDialogGPS.setMessage("Looks like you have not given GPS permissions. Please give GPS permissions and return back to the app.");
            alertDialogGPS.setIcon(android.R.drawable.ic_dialog_alert);
            alertDialogGPS.setButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    Intent intentSettings = new Intent(Settings.ACTION_APPLICATION_SETTINGS);
                    startActivity(intentSettings);
                    alertDialogGPS.dismiss();
                }
            });
            alertDialogGPS.show();
        }
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With