How to get current location of device on google maps api v2?

后端 未结 4 686
走了就别回头了
走了就别回头了 2020-12-17 06:38

I´ve found some methods to do that but are deprecated or doesn´t work. I would like to get current latitude and longitude from device.

Here is how I\'m getting the c

4条回答
  •  情深已故
    2020-12-17 06:48

    This is working correctly remaining all are deprecated

       private FusedLocationProviderClient fusedLocationClient;
     fusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                // TODO: Consider calling
                //    Activity#requestPermissions
                // here to request the missing permissions, and then overriding
                //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                //                                          int[] grantResults)
                // to handle the case where the user grants the permission. See the documentation
                // for Activity#requestPermissions for more details.
                return;
            }
        }
        fusedLocationClient.getLastLocation()
                .addOnSuccessListener(this, new OnSuccessListener() {
                    @Override
                    public void onSuccess(Location location) {
                        // Got last known location. In some rare situations this can be null.
                        if (location != null) {
                            mylatitude = location.getLatitude();
                            mylongitude = location.getLongitude();
                            Log.d("chk", "onSuccess: "+mylongitude);
                            // Logic to handle location object
                        }
                    }
                });
    

    This link will be helpful

提交回复
热议问题