获取片段中的当前纬度和经度

时间:2018-07-14 02:46:17

标签: android gps currentlocation

我试图将我当前的GPS坐标片段化,但是它不起作用。这是我的代码:

public class AjouterBancFragment extends Fragment {

    TextView textViewLongLat;
    LocationManager locationManager;
    String provider;
    Location location;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        final View v = inflater.inflate(R.layout.ajouter_banc, container, false);

        textViewLongLat = v.findViewById(R.id.textViewLongLat);

        // Déclarations
            // Bouton
            Button buttonAdd = v.findViewById(R.id.buttonAjouterBanc);

            buttonAdd.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    locationManager =(LocationManager)getActivity().getSystemService(Context.LOCATION_SERVICE);
                    Criteria c=new Criteria();
                    //if we pass false than
                    //it will check first satellite location than Internet and than Sim Network
                    provider=locationManager.getBestProvider(c, false);
                    if ((ActivityCompat.checkSelfPermission(getContext(), android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED || ActivityCompat.checkSelfPermission(getContext(), android.Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED)) {
                        location=locationManager.getLastKnownLocation(provider);
                    }

                    if(location!=null)
                    {
                        double lng=location.getLongitude();
                        double lat=location.getLatitude();
                        textViewLongLat.setText("Position actuelle : "+lat+","+lng);
                    }
                    else
                    {
                        textViewLongLat.setText("No Provider");
                    }
                }
            });
        return v;
    }
}

问题是此代码给了我2天前所在位置的坐标。 谢谢。

3 个答案:

答案 0 :(得分:1)

不要使用getLastKnownLocation。使用requestSingleUpdate,它将在打开回调之前打开GPS并找到您的当前位置。

答案 1 :(得分:0)

我尝试了另一种方法,因为我不太了解“ requestSingleUpdate”的工作原理 因此,我看了一些使用“ requestLocationUpdates”的教程,但问题是它也不起作用。 这是我的新代码:

// All import stuffs

public class AjouterBancFragment extends Fragment {

    TextView textViewLongLat;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        final View v = inflater.inflate(R.layout.ajouter_banc, container, false);

        textViewLongLat = v.findViewById(R.id.textViewLongLat);
        Button buttonAdd = v.findViewById(R.id.buttonAjouterBanc);

        buttonAdd.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    LocationManager locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
                    LocationListener locationListener = new AjouterBancFragment.MyLocationListener();
                    if ((ActivityCompat.checkSelfPermission(getContext(), android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED || ActivityCompat.checkSelfPermission(getContext(), android.Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED)) {
                        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, locationListener);
                        Toast.makeText(getContext(), "OK", Toast.LENGTH_SHORT).show();   // Toast is showing
                    }

                }
            });
        return v;
    }

    private class MyLocationListener implements LocationListener {

        @Override
        public void onLocationChanged(Location loc) {
            Toast.makeText(getContext(), "OnLocationChanged", Toast.LENGTH_SHORT).show();   // Toast not showing
            String longitude = "Longitude: " + loc.getLongitude();
            String latitude = "Latitude: " + loc.getLatitude();
            String s = longitude + "\n" + latitude;
            textViewLongLat.setText(s);
        }

        @Override
        public void onProviderDisabled(String provider) {
            Toast.makeText(getContext(), "onProviderDisabled", Toast.LENGTH_SHORT).show();  // Toast not showing
        }

        @Override
        public void onProviderEnabled(String provider) {
            Toast.makeText(getContext(), "on ProviderEnabled", Toast.LENGTH_SHORT).show();  // Toast not showing
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            Toast.makeText(getContext(), "onStatusChanged", Toast.LENGTH_SHORT).show();  // Toast not showing
        }
    }
}

我放入Toast Message来查看代码的步骤,但是我只有一个Toast Message出现(在代码中进行了注释)。如果有人知道为什么它不起作用,我很感兴趣! 谢谢。

答案 2 :(得分:0)

我解决了我的问题。当我在家里时,手机未收到GPS信号。我出去了而且行得通