Finding current location of the user in Android

后端 未结 3 998
南笙
南笙 2020-11-29 14:07

I am new to Android and I am trying to develop an Android app which shows current location of the user. I am using Genymotion. Now I am using

mLocation=Loca         


        
3条回答
  •  清歌不尽
    2020-11-29 14:19

         LocationListener locationListener;
            LocationManager locationManager;
            double latitude;
            double longitude;  
    
    
           locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
    
    
            locationListener = new LocationListener()
            {
                @Override
    
    
                public void onLocationChanged(Location location) {
    
        longitude = location.getLongitude();
        latitude = location.getLatitude();
      }
    
                @Override
                public void onStatusChanged(String provider, int status, Bundle extras) {
                }
    
                @Override
                public void onProviderEnabled(String provider) {
                }
    
                @Override
                public void onProviderDisabled(String provider) {
                }
            };
            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.
    
                ;
            }
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000, 0, locationListener);
    
    
    
            Log.e("location", longitude + " " + latitude);
    

提交回复
热议问题