Android locationManager在纬度和经度中返回NULL值

时间:2017-05-19 05:32:06

标签: java android

locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    Criteria criteria = new Criteria();

    mprovider = locationManager.getBestProvider(criteria, false);

    if (mprovider != null && !mprovider.equals("")) {
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            return;
        }
        Location location = locationManager.getLastKnownLocation(mprovider);
        locationManager.requestLocationUpdates(mprovider, 0, 0, SupplierList.this);
       // locationManager.requestLocationUpdates(mprovider, 15000, 1, this);

        if (location != null)
            onLocationChanged(location);
        else
           // showSettingsAlert();
        Toast.makeText(getBaseContext(), "No Location Provider Found Check Your Code", Toast.LENGTH_SHORT).show();
    }

public void onLocationChanged(Location location) {
    Glat =String.valueOf(location.getLatitude());
    Glon =String.valueOf(location.getLongitude());

}

1 个答案:

答案 0 :(得分:0)

代替以mprovider为准

mprovider = locationManager.getBestProvider(criteria,false);

将mprovider作为mprovider = LocationManager.NETWORK_PROVIDER