How to find out why GPS coordinates are missing in Android?

前端 未结 2 536
别跟我提以往
别跟我提以往 2021-01-29 01:55

I am trying to get a device\'s location information using GPS, but for some reason one of the two coordinates is missing sometimes.

This is the code:

Loc         


        
2条回答
  •  醉酒成梦
    2021-01-29 02:46

     Criteria criteria = new Criteria();
        LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
       // mMap.addMarker(new MarkerOptions().position(sydney2).title("fi"));
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#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 ActivityCompat#requestPermissions for more details.
            return;
        }
        locationManager.requestLocationUpdates(locationManager.getBestProvider(criteria, true), 2000, 0, new android.location.LocationListener() {
            @Override
            public void onLocationChanged(Location location) {
               // 
            }
    

    in onLocationChanged method you can use location.getLatitude & location.getLongitude

提交回复
热议问题