Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

smart-location-lib not working on api level 21 and above android #202

Open
kkhurramshahzad opened this issue May 31, 2017 · 1 comment
Open

Comments

@kkhurramshahzad
Copy link

kkhurramshahzad commented May 31, 2017

I am using smart location library. I am trying to get location using longitude and latitude. But my code is working perfectly in api 16. but I checked it on api 21. the same code isnt showing anything.. nor crashing. I have added permissions but still isnt working. Kindly check this out. Help will be appreciated. Thanks.

Can you guys run this and checkout what's the issue.. please

                            public class MainActivity extends AppCompatActivity {

                           private Double lat;
                           private Double lon;
                          private Context context;
                         private Button address_button;
                            private static final int PERMISSION_REQUEST_CODE =11 ;


                             @Override
                          protected void onCreate(Bundle savedInstanceState) {
                      super.onCreate(savedInstanceState);
                       setContentView(R.layout.activity_main);

                      context=this;
                       address_button= (Button) findViewById(R.id.button_address);



                      address_button.setOnClickListener(new View.OnClickListener() {
                       @Override
                     public void onClick(View v) {


        if (Build.VERSION.SDK_INT>= Build.VERSION_CODES.M){
            if (checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION)!= 
                        PackageManager.PERMISSION_GRANTED){
                if 
                (shouldShowRequestPermissionRationale(Manifest.permission.ACCESS_COARSE_LOCATION))
         {
                    new AlertDialog.Builder(MainActivity.this)
                            .setTitle("Call Permission")
                            .setMessage("Hi there! We can't call anyone without the call permission, could you 
       please grant it?")
                            .setPositiveButton("Yep", new DialogInterface.OnClickListener() {
                                @RequiresApi(api = Build.VERSION_CODES.M)
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    requestPermissions(new String[]
      {Manifest.permission.ACCESS_COARSE_LOCATION},PERMISSION_REQUEST_CODE);

                                }
                            })
                            .setNegativeButton("No thanks", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    Toast.makeText(MainActivity.this, ":(", Toast.LENGTH_SHORT).show();                                        
                    }
                            }).show();
                }else {
                    requestPermissions(new String[]
                 {Manifest.permission.ACCESS_COARSE_LOCATION},PERMISSION_REQUEST_CODE);
                }
            } else{
                SmartLocationCityName();
            }
           }else{
            SmartLocationCityName();
           }

         }




         });




             }

                        @Override
                        protected void onStop() {
                          super.onStop();
                         SmartLocation.with(context).location().stop();
                              } 


                          @Override
                           public void onRequestPermissionsResult(int requestCode, @NonNull String[] 
                   permissions, @NonNull 
                   int[] grantResults) {
                   if (requestCode== PERMISSION_REQUEST_CODE){
                if (grantResults.length>0 && grantResults[0]== PackageManager.PERMISSION_GRANTED){
                    SmartLocationCityName();
                }
             }
            }

                  public void SmartLocationCityName(){
                   SmartLocation.with(context).location()
        .start(new OnLocationUpdatedListener() {



            @Override
            public void onLocationUpdated(Location location) {

                lat = location.getLatitude();
                lon = location.getLongitude();
                Toast.makeText(context,lat+" "+lon+" ",Toast.LENGTH_LONG).show();

                try {
                    Geocoder geocoder = new Geocoder(context, Locale.getDefault());
                    List<Address> addresses = geocoder.getFromLocation(lat, lon, 1);
                    String cityName = addresses.get(0).getAddressLine(0);
                    String stateName = addresses.get(0).getAddressLine(1);
                    String countryName = addresses.get(0).getAddressLine(2);

                    Toast.makeText(context,lat+" "+lon+" "+cityName+"-"+"-"+stateName+"-
                  "+countryName,Toast.LENGTH_LONG).show();
                }catch (IOException ex){

                }


            }
        });

          }
                  }
@ReemHazzaa
Copy link

I have the same problem, can anyone help?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants