位置类返回空位置

时间:2018-08-11 02:53:17

标签: java android gps

我是Android开发的新手,希望有人能帮助我。 我的问题如下:

渐变

dependencies {
compile 'com.google.android.gms:play-services-maps:15.0.1'
compile 'com.google.android.gms:play-services-location:15.0.1'
}

MainActivity

[...]
local = (CheckBox) findViewById(R.id.local_checkbox);
local.setOnClickListener(new View.OnClickListener() {
 @Override
        public void onClick(View v) {
        Log.v("DEBUG_INFO","CLICKED");
        GPSTracker g = new GPSTracker(MainActivity.this); 
        if (((CheckBox) v).isChecked()) {
           Log.v("DEBUG_INFO","CHECKED");
           CheckGpsStatus();
            if (GpsStatus == true) {
              isLocated = true;
              Location l = g.getLocation();
              if ((l != null) && (isLocated == true)) {
                  lat = g.getLocation().getLatitude();
                  lon = g.getLocation().getLongitude();
                  Log.v("DEBUG_INFO","DEBUG_INFO: 
                  Latitude: "+ String.valueOf(lat)+ " Longitude: "+ 
                  String.valueOf(lon));
              } 
                  else if ((l != null) && (isLocated == false)) {
                       lat = 0.0;
                       lon = 0.0;
                   } else {
                           lat = 0.0;
                           lon = 0.0;
                     }
            } else {
                    new AlertDialog.Builder(NovaDenunciaActivity.this, R.style.AlertDialogCustom)
                    .setTitle("GPS TEXT!")
                    .setCancelable(false)
                    .setMessage("PLEASE SET GPS TEXT.")
                    .setPositiveButton("CLOSE",new DialogInterface.OnClickListener()
                            {

                            @Override
                             public void onClick(DialogInterface dialog, int which) {
                                    Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                                    startActivity(intent);
                                }
                            }).show();
                    local.toggle();
              }
        } else  {
                  isLocated = false;
                }
        }
  });
  }//onCreate


  public void CheckGpsStatus(){
    locationManager = 
   (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    GpsStatus = 
    locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
    Log.v("CHECKBOX","MARCADO");
  }

} //Code end's

GPSTracker.java

[...]
public class GPSTracker extends Activity implements LocationListener {
Context context;
 /*variables*/
    private TextView latituteField;
    private TextView longitudeField;
    private LocationManager locationManager;
    private String provider;
    public GPSTracker(Context c) {
        context = c;
    }
    /*variables*/
@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    } //Oncreate end's?

public Location getLocation() {
        if (ContextCompat.checkSelfPermission(context,
           Manifest.permission.ACCESS_FINE_LOCATION) != 
           PackageManager.PERMISSION_GRANTED) {
           Toast.makeText(context, "No Permission Text!\nPlease allow 
           permissions text.", Toast.LENGTH_LONG).show();
           return null;
        }

//Get the location manager
locationManager = (LocationManager)
context.getSystemService(context.LOCATION_SERVICE);
// Define the criteria how to select the locatioin provider -> use
// default
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, false);
Location location = locationManager.getLastKnownLocation(provider);
// Initialize the location fields
   if (location != null) {
      System.out.println("Provider " + provider + " has been selected.");
      Log.v("DEBUG_INFO:", "Provider " + provider + " has been selected.");
            onLocationChanged(location);
   } else {
            //latituteField.setText("Location not available");
            //longitudeField.setText("Location not available");
            Log.v("DEBUG_INFO:", "Latitude: not available");
            Log.v("DEBUG_INFO:", "Longitude: not available");

     }
return location;
    }
@Override
    protected void onResume() {
    super.onResume();
    Log.v("DEBUG_INFO:", "REQUESTING LOCATION UPDATES");
    locationManager.requestLocationUpdates(provider, 0, 0, this);
    }
    /* Remove the locationlistener updates when Activity is paused */
    @Override
    protected void onPause() {
        super.onPause();
        locationManager.removeUpdates(this);
        Log.v("DEBUG_INFO:","STOP LOCATION UPDATES");
    }
    @Override
    public void onLocationChanged(Location location) {
        int lat = (int) (location.getLatitude());
        int lng = (int) (location.getLongitude());
        //latituteField.setText(String.valueOf(lat));
        // longitudeField.setText(String.valueOf(lng));
        Log.v("DEBUG_INFO:","Latitude: "+String.valueOf(lat));
        Log.v("DEBUG_INFO:","Longitude: "+String.valueOf(lng));
    }
    @Override
    public void onStatusChanged(String s, int i, Bundle bundle) {
        // TODO Auto-generated method stub
    }
    @Override
    public void onProviderEnabled(String s) {
        Toast.makeText(context, "Enabled new provider " + provider,
                Toast.LENGTH_SHORT).show();
    }
    @Override
    public void onProviderDisabled(String s) {
        Toast.makeText(context, "Disabled provider " + provider,
                Toast.LENGTH_SHORT).show();
    }

}

但是当我单击复选框时,logcat(详细信息)会显示以下内容:

V/DEBUG_INFO:: Latitude: not available
V/DEBUG_INFO:: Longitude: not available  

程序应获得经度和纬度,如果不存在,则应调用locationManager.requestLocationUpdates(provider, 0, 0, this);

但是我不知道我在做什么错。有人可以帮我吗?

0 个答案:

没有答案